Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the dash-question mark syntax in TypeScript?

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?

like image 740
Ahmed Fasih Avatar asked Apr 21 '20 02:04

Ahmed Fasih


People also ask

What is the question mark in TypeScript?

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.

What does () mean in TypeScript?

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.

How do you make a question mark in Teamspeak?

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.


1 Answers

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!

like image 68
jcalz Avatar answered Nov 14 '22 12:11

jcalz