I don't mean the question mark syntax, rather, I'm asking about -?
in, for example:
type Required<T> =
T extends object
? { [P in keyof T]-?: NonNullable<T[P]>; } // <---------- "-?" here
: T;
via this 2018 GitHub comment. I cannot find this syntax in the TypeScript handbook's chapters on advanced types nor utility types.
-?
as above still compiles in TypeScript 3.8, and appears to be the opposite of ?
, i.e., making the key required? Is it equivalent to Required
? If not, what is this syntax called and where can I read more about it?
The question mark ? in typescript is used in two ways: To mention that a particular variable is optional. To pre-check if a member variable is present for an object.
It's coercion to a boolean value. Means that you want to make sure your resulting value is either true or false, not undefined or [ ]. For example, you're passing the resulting value to something that expects a boolean and nothing else. This is how you coerce to a boolean type. 16.
Question marks on TypeScript variable are used to mark that variable as an optional variable. If we put the question mark when declaring a variable that variable becomes optional. The optional parameters will have value as undefined when unused.
You are basically right: it removes the optional property modifier ?
from a mapped type. It was introduced in TypeScript 2.8 as part of improvements to control over mapped type modifiers (see this link for the docs you want). It's not identical to the Required
utility type, but Required
is implemented with it (see library definition here) and could not exist without it.
It's a sad fact that TypeScript documentation is kind of spread out between the handbook, release notes, FAQ, outdated spec, and GitHub issues, without a clear canonical place to look for any particular thing. The language has been evolving more quickly than the documentation can keep up.
Okay, hope that helps; good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With