You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's given that the Undefined type cannot be referenced. This leads to some interesting observations
leta=undefined;// any, unless implicit any is disabledtypeB=typeofundefined;// B is `any` without a warning and regardless of what you think implicit any isletb : B;// anyletc=Math.random()>0.5 ? b : '';// anyletd=Math.random()>0.5 ? undefined : '';// stringlete=Math.random()>0.5 ? <any>'' : '';// anyfunctionfn(){returnundefined;}// problem unless implicit any is allowedletf=Math.random()>0.5 ? fn() : '';// any
Basically turns out that undefined is a literal without any type assigned (so is null), despite being a variable in a non-strict mode.
The following case is extremely useful in practice
letd=Math.random()>0.5 ? undefined : '';// string
Because instead of finding the common denominator type the inference craves a type for the "undefined" branch from the expression context. I wish it was the case for the following too, wouldn't you?
The undefined and null types are only used for intermediary results in expressions. There is no way to name them, and we perform widening (i.e. conversion to the any type) in places where we might infer the undefined and null types. This is by design as you'd be able to do practically nothing with values and variables of those types.
That said, we have discussed not widening function result types (e.g. here), but it is not without complications.
this doesn't necessarily have anything to do with inference on undefined. This example is a problem because f() gives you the {} type which is a supertype of string. We then try to reduce the union of this conditional expression {} | string to {}.
For the same reason in the following example
letd=Math.random()>0.5 ? undefined : '';// string
we don't immediately widen the type of undefined, so you get undefined | string, which reduces to string.
It's given that the
Undefined
type cannot be referenced. This leads to some interesting observationsBasically turns out that
undefined
is a literal without any type assigned (so isnull
), despite being a variable in a non-strict mode.The following case is extremely useful in practice
Because instead of finding the common denominator type the inference craves a type for the "undefined" branch from the expression context. I wish it was the case for the following too, wouldn't you?
I would be grateful if someone from the design team shared some insights why it is this way, or gave a link that sorts it all out.
The text was updated successfully, but these errors were encountered: