Skip to content

When using the @Groups decorator in models - how can I make the TS type correct for each group? #2304

Answered by Romakita
markusenglund asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @markusenglund

When you use a complex interface like "T extends 'create' ? undefined : string;" , typescript doesn't set String as metadata but Object.

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:

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
Romain

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Romakita
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants