What does the “-?” syntax mean in TypeScript mapped types?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



What does the “-?” syntax mean in TypeScript mapped types?



Can anybody please explain what that "-?" in the following TypeScript type declarations
means, compared to just using a "?" there?


type ValidationMap<T> = [K in keyof T]-?: Validator<T[K]>




1 Answer
1



It's not a wildcard. The -? notation was added in TypeScript 2.8 as a way of removing the optional modifier from mapped types.


-?



Basically, it means that ValidationMap<T> has the same keys as T, but none of the properties of ValidationMap<T> is optional, even if the corresponding property of T is. For example:


ValidationMap<T>


T


ValidationMap<T>


T


type Example = ValidationMap<a: string, b?: number >;
// type Example = a: Validator<string>, b: Validator<number



Here, Example has a required b property even though the type it's mapped from has an optional b property.


Example


b


b



(Note that if you change -? to just ? or to +?, it becomes the opposite... you would be adding the optional modifier. The ? notation was added in TypeScript 2.1 as part of the mapped types feature, while the +? notation was introduced in TypeScript 2.8 along with -?.)


-?


?


+?


?


+?


-?



Hope that helps. Good luck!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS