Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -? mean in TypeScript?

Tags:

typescript

I came across this line in the type definitions for prop-types:

export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> }; 

Without - it would be a pretty standard partial mapped type, but I can't find anywhere in the docs where it talks about -?.

Can anyone explain what -? means?

like image 571
Brian Adams Avatar asked Sep 20 '18 03:09

Brian Adams


People also ask

What does ?: In TypeScript mean?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

What is '!' In TypeScript?

What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !

What does !: Means in angular?

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths.


Video Answer


1 Answers

+ or - allows control over the mapped type modifier (? or readonly). -? means must be all present, aka it removes optionality (?) e.g.:

type T = {     a: string     b?: string }   // Note b is optional const sameAsT: { [K in keyof T]: string } = {     a: 'asdf', // a is required }  // Note a became optional const canBeNotPresent: { [K in keyof T]?: string } = { }  // Note b became required const mustBePreset: { [K in keyof T]-?: string } = {     a: 'asdf',      b: 'asdf'  // b became required  } 

More

I did a lesson on these mapped type modifiers : https://www.youtube.com/watch?v=0zgWo_gnzVI 🌹

like image 72
basarat Avatar answered Sep 22 '22 12:09

basarat