What does the `-?` (dash question) and `-readonly` (dash readonly) syntax mean in TypeScript?
Asked Answered
A

1

16

I am new to TypeScript and saw the following in the @types/prop-types package. What does -? mean in [K in keyof T]-??

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

Similarly, what does -readonly [K in keyof T]? mean in immer?

export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}

I tried my best to google that, but failed.

Thank you.


Extra keywords: hyphen, minus, grammar

Attaint answered 10/9, 2018 at 8:47 Comment(1)
Doesn't seem to be a newbie question. You're looking inside a package. Why do you undervalue yourself?Granvillegranvillebarker
N
19

Homomorphic mapped types copy the optionality of the original type field to the mapped type field. The -? is the syntax used to explicitly remove any optional modifier from the resulting mapped type.

Similarly, -readonly removes the "readonlyness".

You can read more details here.

Nena answered 10/9, 2018 at 8:49 Comment(5)
@Attaint Welcome to SO! If this answer is correct, you should make it the accepted answer: stackoverflow.com/help/someone-answers.Encasement
I've been looking for a way of doing this for so long!Misguided
As of late 2020, the Handbook still doesn't document this syntax, except this blurb in the 2.8 release notes. There's an open issue to get the docs in the right place.Daphnedaphnis
@Daphnedaphnis the new docs are definitely improved, but there is also lots of room for improvement 😉Nena
Since you're here, did you happen to see my other question? That's what led me to this one.Daphnedaphnis

© 2022 - 2024 — McMap. All rights reserved.