Why am I getting different type inference here for the array ?
const a = []; // any[]
const b = null ?? []; // never[]
Demo here
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.
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.
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.
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.
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 ;
"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.
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'
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.
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.
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