Skip to content

Type to invert optionality of properties #16173

Description

@CzBuCHi

add new mapped type: Invert<T> ... I do not know, how to explain it in english, so i use code:

// proposal syntaxtypeInvert<T>={[PinkeyofT]?: T[P];[P? inkeyofT]: T[P];};// usagetypeOriginal={first: any;second?: any;}typeInverted=Invert<Original>;// type Inverted is threated as if declared like this:typeInverted={first?: any;// first is optional becasue it was required in Originalsecond: any;// second is required becasue it was optional in Original}

example usage: react defaultProps - to ensure, that all optional props are declared.

typeSomeProps={name: string;point?: {x: number,y: number};}classSomeextendsReact.Component<SomeProps,void>{// proposal usagestaticdefaultProps: Invert<SomeProps>={// here i get compiler error if do not specify property 'point'};// current usage (worst)staticdefaultProps: any={// here i dont get any errors at all};// betterstaticdefaultProps: Partial<SomeProps>={// here i dont get any errors if i dont specify point// but at least get compiler error for typo...};// even betterstaticdefaultProps: {name?: string;point: {x: number,y: number}}={// here i get compiler error if do not specify property 'point' and for typo// but i was forced to create new type ...};}

priority: really low, almost negative :) - but if it can be done and isnt too hard ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions