Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a = null ?? [] of type never[]?

Why am I getting different type inference here for the array ?

const a = []; // any[]
const b = null ?? []; // never[] 

Demo here

like image 581
Matthieu Riegler Avatar asked Dec 21 '21 14:12

Matthieu Riegler


People also ask

What does never [] mean in TypeScript?

TypeScript introduced a new type never , which indicates the values that will never occur. The never type is used when you are sure that something is never going to occur. For example, you write a function which will not return to its end point or always throws an exception. Example: never.

How do you fix does not exist on type never?

The error "Property does not exist on type 'never'" occurs when we forget to type a state array or don't type the return value of the useRef hook. To solve the error, use a generic to explicitly type the state array or the ref value in your React application.

Which of the following types can be assignable to the never data type?

the never type represents the type of values that never occur: The Typescript compiler assigns types to all your variables/properties. These types can be either the typescript defined types like number and string , or user defined types made with the interface and type keyword.

What is the difference between never and void in TypeScript?

There is a difference between void and never. A function that has the explicit return type of never won't allow returning undefined, which is different from a void function which allows returning undefined.

Why can't int have null in it?

Because int is a value type rather than a reference type. The C# Language Specification doesn't allow an int to contain null. Try compiling this statement: int x = null ;

Can a value type have a null value?

"Value types" in .NET (like int, double, and bool) cannot, by definition, be null - they always have an actual value assigned. Check out this good intro to value types vs. reference types. The usage of NULL applies to Pointers and References in general. A value 0 assigned to an integer is not null.

What is the difference between never type and void type?

Thus, never type is used to indicate the value that will never occur or return from a function. The void type can have undefined or null as a value where as never cannot have any value. let something: void = null; let nothing: never = null; // Error: Type 'null' is not assignable to type 'never'

How to avoid nullable errors in typescript?

Typescript is a statically typed version of javascript, So the strictNullCheck configuration in tsconfig.json allows to avoid nullable errors. strictNullCheck=false - null and undefined are subtypes of an existing type, so we can assign them without error. Declared a string variable and can be assigned with string, null, and undefined.


Video Answer


1 Answers

The "correct" thing to do would be to infer never[] for the empty array literal [], because the type of an array literal is the join of the types of its elements, and never is the join of an empty set.

However, since inferring never[] is usually not what the user intends when writing const a = [], this particular case received a very special treatment in the compiler, and now it starts by implicitly inferring any[], and then refining the type based on the subsequent control flow.

There does not seem to be any deeper meaning behind it: it's just what turns out to be the most useful in most cases.

like image 110
Andrey Tyukin Avatar answered Oct 19 '22 13:10

Andrey Tyukin