typescript source code use undefined!
in many places. For example, in binder.ts, from line 261 to 271:
file = undefined!;
options = undefined!;
languageVersion = undefined!;
parent = undefined!;
container = undefined!;
thisParentContainer = undefined!;
blockScopeContainer = undefined!;
lastContainer = undefined!;
delayedTypeAliases = undefined!;
seenThisKeyword = false;
currentFlow = undefined!;
From typescript official docs, the postfix !
means "Non-null assertion operator", and it's definition is that:
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
So this usage undefined!
seems make no sense, because it asserts that undefined is non-undefined.
What is the meaning of undefined!
, and why we use in that way ?
The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. So, the value of the variable is 'undefined'.
null means no value. To make a variable null we must assign null value to it as by default in typescript unassigned values are termed undefined. We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
The "Object is possibly 'undefined'" error occurs when we try to access a property on an object that may have a value of undefined . To solve the error, use the optional chaining operator or a type guard to make sure the reference is not undefined before accessing properties.
To avoid undefined values when using or accessing the optional object properties, the basic idea is to check the property value using an if conditional statement or the optional chaining operator before accessing the object property in TypeScript.
The reason for this is that, prior to version 2 of TypeScript, null and undefined were actually what’s called a “subtype” of every other type. This means that null was assignable to any other type including number s, string s etc. As of TypeScript 2 however, we have the concept of “non-nullable types”.
More examples below. The undefined property indicates that a variable has not been assigned a value, or not declared at all. undefined () is an ECMAScript1 (ES1) feature. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
The undefined is applicable to variables of any data type. The undefined variables can be of use only when working with union types; otherwise, they are useless because they can be assigned only one undefined value.
The exact same thing is true for undefined under this “non-nullable” types umbrella: undefined now has its own distinct type, which is not assignable to anything else. It is also worth explicitly noting that undefined and null types are therefore also not assignable to each other.
So this usage undefined! seems make no sense, because it asserts that undefined is non-undefined.
What is the meaning of undefined!, and why we use in that way ?
Another way to put it is, telling typescript "Shut up, I know what I'm doing". If strictNullChecks
is on, Typescript will complain when assigning undefined
/null
to a value with a type that doesn't include undefined
/null
.
strictNullChecks
is a good default, but there are cases where you might want to assign undefined
or null
anyway (maybe in a local scope or private part of a library), and you yourself guarantee that you're always going ensure a value is set later.
The benefit is that the user of the library doesn't have to deal with optional properties, and you, as the library author, might have a bit more flexibility about how objects are built before they leave the boundary of your library.
Example:
type MyArticle = {
title: string;
}
function getArticle(): MyArticle {
const result:MyArticle = {
// ignore the undefined error, we're dealing with this later.
title: undefined!,
};
result.title = 'Hello world';
return result;
}
The above example is contrived. There's better ways to structure it, and I suspect that that's true for the sample you shared as well.
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