Skip to content

Element access assignment uses declared type, not narrowed type #27574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2018
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
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9305,7 +9305,9 @@ namespace ts {
}
}
const propType = getTypeOfSymbol(prop);
return accessExpression ? getFlowTypeOfReference(accessExpression, propType) : propType;
return accessExpression && getAssignmentTargetKind(accessExpression) !== AssignmentKind.Definite ?
getFlowTypeOfReference(accessExpression, propType) :
propType;
}
if (isTupleType(objectType)) {
const restType = getRestTypeOfTupleType(objectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var __extends = (this && this.__extends) || (function () {
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/controlFlowElementAccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [controlFlowElementAccess.ts]
let x: { o: boolean } = { o: false }
if (x['o'] === false) {
x['o'] = true
}

const y: [number, number] = [0, 0];
if (y[0] === 0) {
y[0] = -1;
}


//// [controlFlowElementAccess.js]
var x = { o: false };
if (x['o'] === false) {
x['o'] = true;
}
var y = [0, 0];
if (y[0] === 0) {
y[0] = -1;
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/controlFlowElementAccess.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/controlFlow/controlFlowElementAccess.ts ===
let x: { o: boolean } = { o: false }
>x : Symbol(x, Decl(controlFlowElementAccess.ts, 0, 3))
>o : Symbol(o, Decl(controlFlowElementAccess.ts, 0, 8))
>o : Symbol(o, Decl(controlFlowElementAccess.ts, 0, 25))

if (x['o'] === false) {
>x : Symbol(x, Decl(controlFlowElementAccess.ts, 0, 3))
>'o' : Symbol(o, Decl(controlFlowElementAccess.ts, 0, 8))

x['o'] = true
>x : Symbol(x, Decl(controlFlowElementAccess.ts, 0, 3))
>'o' : Symbol(o, Decl(controlFlowElementAccess.ts, 0, 8))
}

const y: [number, number] = [0, 0];
>y : Symbol(y, Decl(controlFlowElementAccess.ts, 5, 5))

if (y[0] === 0) {
>y : Symbol(y, Decl(controlFlowElementAccess.ts, 5, 5))
>0 : Symbol(0)

y[0] = -1;
>y : Symbol(y, Decl(controlFlowElementAccess.ts, 5, 5))
>0 : Symbol(0)
}

45 changes: 45 additions & 0 deletions tests/baselines/reference/controlFlowElementAccess.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/conformance/controlFlow/controlFlowElementAccess.ts ===
let x: { o: boolean } = { o: false }
>x : { o: boolean; }
>o : boolean
>{ o: false } : { o: false; }
>o : false
>false : false

if (x['o'] === false) {
>x['o'] === false : boolean
>x['o'] : boolean
>x : { o: boolean; }
>'o' : "o"
>false : false

x['o'] = true
>x['o'] = true : true
>x['o'] : boolean
>x : { o: boolean; }
>'o' : "o"
>true : true
}

const y: [number, number] = [0, 0];
>y : [number, number]
>[0, 0] : [number, number]
>0 : 0
>0 : 0

if (y[0] === 0) {
>y[0] === 0 : boolean
>y[0] : number
>y : [number, number]
>0 : 0
>0 : 0

y[0] = -1;
>y[0] = -1 : -1
>y[0] : number
>y : [number, number]
>0 : 0
>-1 : -1
>1 : 1
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(11,3): error TS2339: Property 'bar' does not exist on type 'Missing'.
Property 'bar' does not exist on type '{ [s: string]: string; }'.
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(14,4): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property.
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(18,1): error TS2322: Type '"ok"' is not assignable to type 'number'.
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(24,1): error TS7017: Element implicitly has an 'any' type because type 'Both' has no index signature.
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(25,1): error TS2322: Type '"not ok"' is not assignable to type 'number'.
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(26,1): error TS7017: Element implicitly has an 'any' type because type 'Both' has no index signature.


==== tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts (6 errors) ====
==== tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts (5 errors) ====
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string };
declare var u: Two
u.foo = 'bye'
Expand All @@ -31,8 +30,6 @@ tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(26,1): error
declare var num: Num
num[0] = 1
num['0'] = 'ok'
~~~~~~~~
!!! error TS2322: Type '"ok"' is not assignable to type 'number'.
const sym = Symbol()
type Both = { s: number, '0': number, [sym]: boolean } | { [n: number]: number, [s: string]: string | number }
declare var both: Both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ num[0] = 1

num['0'] = 'ok'
>num['0'] = 'ok' : "ok"
>num['0'] : number
>num['0'] : string | number
>num : Num
>'0' : "0"
>'ok' : "ok"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let x: { o: boolean } = { o: false }
if (x['o'] === false) {
x['o'] = true
}

const y: [number, number] = [0, 0];
if (y[0] === 0) {
y[0] = -1;
}