When using the @Groups decorator in models - how can I make the TS type correct for each group? #2304
-
Hey! Let's say I have a model that looks like this: export class Example {
@Required()
@RequiredGroups('!action.create')
id?: string;
@Required()
content: string;
constructor(options: any) {
this.content = options.content;
this.id = options.id
}
} The export class Example<T extends 'create' | 'get'> {
@Required()
@RequiredGroups('!action.create')
id: T extends 'create' ? undefined : string;
@Required()
content: string;
constructor(options: any) {
this.content = options.content;
this.platform = options.platform;
}
} However, the generated OpenAPI spec now says Any ideas for good solution? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @markusenglund When you use a complex interface like "T extends 'create' ? undefined : string;" , typescript doesn't set This is why Ts.ED isn't able to infer the correct type and this is why Ts.ED provide export class Example<T extends 'create' | 'get'> {
@Property(String)
@Required()
@RequiredGroups('!action.create')
id: T extends 'create' ? undefined : string;
@Required()
content: string;
constructor(options: any) {
this.content = options.content;
this.platform = options.platform;
}
} See you |
Beta Was this translation helpful? Give feedback.
Hello @markusenglund
When you use a complex interface like "T extends 'create' ? undefined : string;" , typescript doesn't set
String
as metadata butObject
.This is why Ts.ED isn't able to infer the correct type and this is why Ts.ED provide
@Property
decorator ;). The correct code is:See you
Romain