'Importing complex types - NestJS tsconfig.json
I am creating a full-stack application and I'm trying to standardize the communication between the front-end and the back-end. The back-end is being built using NestJS and Prisma.
Thus, I have created some complex Typescript types, trying to transform some of my data.
Specifically, I have a util.ts file outside the back-end's src folder (in order to share it) with below complex types:
/** Returns only non optional fields */
export type NoUndefined<T> = {
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]
}
/** Returns only nullable fields */
export type OnlyNull<T> = {
[K in keyof T as T[K] extends Exclude<T[K], null> ? never : K]: T[K]
}
/** Returns only non optional and non null fields */
export type NoNullUndefined<T> = {
[K in keyof T as T[K] extends Exclude<T[K], undefined | null> ? K : never]: T[K]
}
export type NoNullUndefined_NullToOptional<T> = NoNullUndefined<T> & Partial<NoUndefined<OnlyNull<T>>>
Let's say for example I have the object:
const before: {
id: number,
name: string,
nullable_field: string | null,
optional_field?: string
} = ...
The type of NoNullUndefined_NullToOptional<typeof before> will be
{
id: number,
name: string,
nullable_field?: string | null,
}
When I am using said transformations in the same util.ts VSCode infers the type correctly. The same goes for when I'm importing them in the front-end project. That's awesome.
When I am importing the types and using them inside the NestJS project, though, VSCode infers the original (before, T) type. So even if I declare
const after: NoNullUndefined_NullToOptional<Users> = ...
VSCode and the NestJS compiler treat the object as a Users type and create errors.
I am guessing this is a tsconfig.json issue, but I don't know enough to figure out what's wrong.
I'm providing the back-end's tsconfig.json below:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"paths": {
"@models/*": [
"../prisma/generated/*"
],
"@sharedTypes/*": [
"../shared/*"
]
}
}
}
Any ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
