Thursday, 21 January 2021

What does `undefined!` means in typescript?

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 ?



from What does `undefined!` means in typescript?

No comments:

Post a Comment