What does -? mean in TypeScript?

Viewed 11015

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?

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

Related