It is to mark the parameter as optional.
- TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters
- TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters
It is to mark the parameter as optional.
- TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters
- TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters
parameter?: type is a shorthand for parameter: type | undefined
So what is the difference? Question mark means "optional".
More precisely parameter?: type is equal to parameter: type | undefined = undefined
I am learning react, and was watching a react video tutorial and got stuck.
The person writes a question mark next to movies, like this "movies?" please see below. I don't understand what it means and what difference it makes if he put "movies.length > 0" instead.
{
movies?.length > 0
? (
) : (
)
}I understand what happens after the 2nd question mark, just don't understand the first one. Hope someone can assist, thanks!
Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:
At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a
nullorundefined. The star of the show in optional chaining is the new?.operator for optional property accesses.
Refer to the TypeScript 3.7 release notes for more details.
Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).
As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript and Swift), there are a couple of other examples:
- CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):
The accessor variant of the existential operator
?.can be used to soak up null references in a chain of properties. Use it instead of the dot accessor.in cases where the base value may be null or undefined.
- C# calls this a null-conditional operator.
a null-conditional operator applies a member access,
?., or element access,?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returnsnull.
- Kotlin refers to it as the safe call operator.
There are probably lots of other examples, too.
It is now possible, see answer of user "Donut".
Old answer: Standard JavaScript behaviour regarding boolean operators has something that may help. The boolean methods do not return true or false when comparing objects, but in case of OR the first value that is equal to true.
Not as nice as a single ?, but it works:
var thing = foo && foo.bar || null;
You can use as many && as you like:
var thing = foo && foo.bar && foo.bar.check && foo.bar.check.x || null;
Default values are also possible:
var name = person && person.name || "Unknown user";
That's the non-null assertion operator. It is a way to tell the compiler "this expression cannot be null or undefined here, so don't complain about the possibility of it being null or undefined." Sometimes the type checker is unable to make that determination itself.
It is explained in the TypeScript release notes:
A new
!post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operationx!produces a value of the type ofxwithnullandundefinedexcluded. Similar to type assertions of the forms<T>xandx as T, the!non-null assertion operator is simply removed in the emitted JavaScript code.
I find the use of the term "assert" a bit misleading in that explanation. It is "assert" in the sense that the developer is asserting it, not in the sense that a test is going to be performed. The last line indeed indicates that it results in no JavaScript code being emitted.
Louis' answer is great, but I thought I would try to sum it up succinctly:
The bang operator tells the compiler to temporarily relax the "not null" constraint that it might otherwise demand. It says to the compiler: "As the developer, I know better than you that this variable cannot be null right now".
I’m calling a rest api using react query and it’s returning me an array of objects. I have already defined the type for that object and i’m trying to transform that response array into something else using .map or .reduce.
I have the condition for isLoading or isIdle present. So i’m assuming that after that code block, the data would be present. Is my assumption flawed?
I’m using a ternary in my return jsx function and there’s no error on my end so far. So is the question mark bad?