diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f48ad223ffb..eae8fc33525 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16863,7 +16863,7 @@ namespace ts { function checkReferenceExpression(expr: Expression, invalidReferenceMessage: DiagnosticMessage): boolean { // References are combinations of identifiers, parentheses, and property accesses. - const node = skipParentheses(expr); + const node = skipOuterExpressions(expr, OuterExpressionKinds.Assertions | OuterExpressionKinds.Parentheses); if (node.kind !== SyntaxKind.Identifier && node.kind !== SyntaxKind.PropertyAccessExpression && node.kind !== SyntaxKind.ElementAccessExpression) { error(expr, invalidReferenceMessage); return false; diff --git a/tests/baselines/reference/incrementOnNullAssertion.js b/tests/baselines/reference/incrementOnNullAssertion.js new file mode 100644 index 00000000000..1f56fa00f56 --- /dev/null +++ b/tests/baselines/reference/incrementOnNullAssertion.js @@ -0,0 +1,28 @@ +//// [incrementOnNullAssertion.ts] +interface Dictionary { + [myFavouriteType: string]: T | undefined +} +const x = 'bar' +let foo: Dictionary = {} +if (foo[x] === undefined) { + foo[x] = 1 +} +else { + let nu = foo[x] + let n = foo[x] + foo[x]!++ +} + + +//// [incrementOnNullAssertion.js] +"use strict"; +var x = 'bar'; +var foo = {}; +if (foo[x] === undefined) { + foo[x] = 1; +} +else { + var nu = foo[x]; + var n = foo[x]; + foo[x]++; +} diff --git a/tests/baselines/reference/incrementOnNullAssertion.symbols b/tests/baselines/reference/incrementOnNullAssertion.symbols new file mode 100644 index 00000000000..07f37e4aaa7 --- /dev/null +++ b/tests/baselines/reference/incrementOnNullAssertion.symbols @@ -0,0 +1,41 @@ +=== tests/cases/compiler/incrementOnNullAssertion.ts === +interface Dictionary { +>Dictionary : Symbol(Dictionary, Decl(incrementOnNullAssertion.ts, 0, 0)) +>T : Symbol(T, Decl(incrementOnNullAssertion.ts, 0, 21)) + + [myFavouriteType: string]: T | undefined +>myFavouriteType : Symbol(myFavouriteType, Decl(incrementOnNullAssertion.ts, 1, 5)) +>T : Symbol(T, Decl(incrementOnNullAssertion.ts, 0, 21)) +} +const x = 'bar' +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) + +let foo: Dictionary = {} +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>Dictionary : Symbol(Dictionary, Decl(incrementOnNullAssertion.ts, 0, 0)) + +if (foo[x] === undefined) { +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) +>undefined : Symbol(undefined) + + foo[x] = 1 +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) +} +else { + let nu = foo[x] +>nu : Symbol(nu, Decl(incrementOnNullAssertion.ts, 9, 7)) +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) + + let n = foo[x] +>n : Symbol(n, Decl(incrementOnNullAssertion.ts, 10, 7)) +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) + + foo[x]!++ +>foo : Symbol(foo, Decl(incrementOnNullAssertion.ts, 4, 3)) +>x : Symbol(x, Decl(incrementOnNullAssertion.ts, 3, 5)) +} + diff --git a/tests/baselines/reference/incrementOnNullAssertion.types b/tests/baselines/reference/incrementOnNullAssertion.types new file mode 100644 index 00000000000..f6acdda65bc --- /dev/null +++ b/tests/baselines/reference/incrementOnNullAssertion.types @@ -0,0 +1,53 @@ +=== tests/cases/compiler/incrementOnNullAssertion.ts === +interface Dictionary { +>Dictionary : Dictionary +>T : T + + [myFavouriteType: string]: T | undefined +>myFavouriteType : string +>T : T +} +const x = 'bar' +>x : "bar" +>'bar' : "bar" + +let foo: Dictionary = {} +>foo : Dictionary +>Dictionary : Dictionary +>{} : {} + +if (foo[x] === undefined) { +>foo[x] === undefined : boolean +>foo[x] : number | undefined +>foo : Dictionary +>x : "bar" +>undefined : undefined + + foo[x] = 1 +>foo[x] = 1 : 1 +>foo[x] : number | undefined +>foo : Dictionary +>x : "bar" +>1 : 1 +} +else { + let nu = foo[x] +>nu : number | undefined +>foo[x] : number | undefined +>foo : Dictionary +>x : "bar" + + let n = foo[x] +>n : number | undefined +>foo[x] : number | undefined +>foo : Dictionary +>x : "bar" + + foo[x]!++ +>foo[x]!++ : number +>foo[x]! : number +>foo[x] : number | undefined +>foo : Dictionary +>x : "bar" +} + diff --git a/tests/cases/compiler/incrementOnNullAssertion.ts b/tests/cases/compiler/incrementOnNullAssertion.ts new file mode 100644 index 00000000000..4521b8a4455 --- /dev/null +++ b/tests/cases/compiler/incrementOnNullAssertion.ts @@ -0,0 +1,14 @@ +// @strict: true +interface Dictionary { + [myFavouriteType: string]: T | undefined +} +const x = 'bar' +let foo: Dictionary = {} +if (foo[x] === undefined) { + foo[x] = 1 +} +else { + let nu = foo[x] + let n = foo[x] + foo[x]!++ +}