Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16880,7 +16880,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;
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/incrementOnNullAssertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [incrementOnNullAssertion.ts]
interface Dictionary<T> {
[myFavouriteType: string]: T | undefined
}
const x = 'bar'
let foo: Dictionary<number> = {}
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]++;
}
41 changes: 41 additions & 0 deletions tests/baselines/reference/incrementOnNullAssertion.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
=== tests/cases/compiler/incrementOnNullAssertion.ts ===
interface Dictionary<T> {
>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<number> = {}
>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))
}

53 changes: 53 additions & 0 deletions tests/baselines/reference/incrementOnNullAssertion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/incrementOnNullAssertion.ts ===
interface Dictionary<T> {
>Dictionary : Dictionary<T>
>T : T

[myFavouriteType: string]: T | undefined
>myFavouriteType : string
>T : T
}
const x = 'bar'
>x : "bar"
>'bar' : "bar"

let foo: Dictionary<number> = {}
>foo : Dictionary<number>
>Dictionary : Dictionary<T>
>{} : {}

if (foo[x] === undefined) {
>foo[x] === undefined : boolean
>foo[x] : number | undefined
>foo : Dictionary<number>
>x : "bar"
>undefined : undefined

foo[x] = 1
>foo[x] = 1 : 1
>foo[x] : number | undefined
>foo : Dictionary<number>
>x : "bar"
>1 : 1
}
else {
let nu = foo[x]
>nu : number | undefined
>foo[x] : number | undefined
>foo : Dictionary<number>
>x : "bar"

let n = foo[x]
>n : number | undefined
>foo[x] : number | undefined
>foo : Dictionary<number>
>x : "bar"

foo[x]!++
>foo[x]!++ : number
>foo[x]! : number
>foo[x] : number | undefined
>foo : Dictionary<number>
>x : "bar"
}

14 changes: 14 additions & 0 deletions tests/cases/compiler/incrementOnNullAssertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
interface Dictionary<T> {
[myFavouriteType: string]: T | undefined
}
const x = 'bar'
let foo: Dictionary<number> = {}
if (foo[x] === undefined) {
foo[x] = 1
}
else {
let nu = foo[x]
let n = foo[x]
foo[x]!++
}