microsoft/TypeScript

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

SamPruden opened this issue ยท 4 comments

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.

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.