Typescript: '!' allowed in T["prop"!]

Created on 4 Aug 2017  路  4Comments  路  Source: microsoft/TypeScript

TypeScript Version: 2.4.2
Code

type Huh = any["whatever"!];

Expected behavior:
Syntax error.

Actual behavior:
This appears to work exactly as it would without the ! character.

I'm guessing that this is getting parsed as a non-null assertion. I might be wrong, maybe this is completely valid, but I was under the impression that only a string literal should be allowed in that position.

I don't know if this causes any problems, I just typed it by accident and was surprised by the lack of red squigglies.

Bug Fixed

All 4 comments

We have a postfix ! operator called the non-null assertion operator. It removes null | undefined types from the expression on the left of it.

declare const x: string | undefined;

// Error!
x.toLowerCase()


// Okay!
x!.toLowerCase()

@DanielRosenwasser why is it allowed in a type context?

Whoops, misread. Probably related to #12146 but I won't mark it as a dupe just yet.

should be fixed now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rwyborn picture rwyborn  路  210Comments

yortus picture yortus  路  157Comments

xealot picture xealot  路  150Comments

Gaelan picture Gaelan  路  231Comments

disshishkov picture disshishkov  路  224Comments