Skip to content

Narrow object property when key is a variable #57274

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

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 16 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26474,6 +26474,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const key = getFlowCacheKey((node as AccessExpression).expression, declaredType, initialType, flowContainer);
return key && key + "." + propName;
}
else if (isElementAccessExpression(node) && isConstantReference(node.argumentExpression)) {
const propertyCacheKey = getFlowCacheKey(node.argumentExpression, unknownType, unknownType, flowContainer);
if (!propertyCacheKey) {
return undefined;
}
const key = getFlowCacheKey((node as AccessExpression).expression, declaredType, initialType, flowContainer);
return key && key + "[" + propertyCacheKey + "]";
}
break;
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.ArrayBindingPattern:
Expand Down Expand Up @@ -26519,8 +26527,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.ElementAccessExpression:
const sourcePropertyName = getAccessedPropertyName(source as AccessExpression);
const targetPropertyName = isAccessExpression(target) ? getAccessedPropertyName(target) : undefined;
return sourcePropertyName !== undefined && targetPropertyName !== undefined && targetPropertyName === sourcePropertyName &&
isMatchingReference((source as AccessExpression).expression, (target as AccessExpression).expression);
if (sourcePropertyName !== undefined || targetPropertyName !== undefined) {
return sourcePropertyName !== undefined && targetPropertyName !== undefined && targetPropertyName === sourcePropertyName &&
isMatchingReference((source as AccessExpression).expression, (target as AccessExpression).expression);
}
else {
return isElementAccessExpression(source) && isElementAccessExpression(target) && isMatchingReference(source.expression, target.expression) &&
isMatchingReference(source.argumentExpression, target.argumentExpression) && isConstantReference(source.argumentExpression) && isConstantReference(target.argumentExpression);
}
case SyntaxKind.QualifiedName:
return isAccessExpression(target) &&
(source as QualifiedName).right.escapedText === getAccessedPropertyName(target) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class Test {
>[] : never[]
}
this.entries[name]?.push(entry);
>this.entries[name]?.push(entry) : number | undefined
>this.entries[name]?.push : ((...items: Types[T][]) => number) | undefined
>this.entries[name] : Types[T][] | undefined
>this.entries[name]?.push(entry) : number
>this.entries[name]?.push : (...items: Types[T][]) => number
>this.entries[name] : Types[T][]
>this.entries : { first?: { a1: true; }[] | undefined; second?: { a2: true; }[] | undefined; third?: { a3: true; }[] | undefined; }
>this : this
>entries : { first?: { a1: true; }[] | undefined; second?: { a2: true; }[] | undefined; third?: { a3: true; }[] | undefined; }
>name : T
>push : ((...items: Types[T][]) => number) | undefined
>push : (...items: Types[T][]) => number
>entry : Types[T]
}
}
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/noUncheckedIndexedAccess.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ noUncheckedIndexedAccess.ts(90,7): error TS2322: Type 'string | undefined' is no
Type 'undefined' is not assignable to type 'string'.
noUncheckedIndexedAccess.ts(98,5): error TS2322: Type 'undefined' is not assignable to type '{ [key: string]: string; a: string; b: string; }[Key]'.
Type 'undefined' is not assignable to type 'string'.
noUncheckedIndexedAccess.ts(99,11): error TS2322: Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
noUncheckedIndexedAccess.ts(99,11): error TS2322: Type 'undefined' is not assignable to type 'string'.


==== noUncheckedIndexedAccess.ts (31 errors) ====
Expand Down Expand Up @@ -203,8 +202,7 @@ noUncheckedIndexedAccess.ts(99,11): error TS2322: Type 'string | undefined' is n
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
const v: string = myRecord2[key]; // Should error
~
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};


2 changes: 1 addition & 1 deletion tests/baselines/reference/noUncheckedIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {

const v: string = myRecord2[key]; // Should error
>v : string
>myRecord2[key] : string | undefined
>myRecord2[key] : undefined
>myRecord2 : { [key: string]: string; a: string; b: string; }
>key : Key

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
typeGuardNarrowsIndexedAccessOfAnyProperty.ts(51,7): error TS2532: Object is possibly 'undefined'.


==== typeGuardNarrowsIndexedAccessOfAnyProperty.ts (1 errors) ====
namespace Problem1 {
declare const obj: { [key: string]: string | undefined };
declare let key: "a";
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem2 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem3 {
declare const obj: { a?: string, b?: string };
declare const key: "a" | "b";
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem4 {
function f<K extends string>(obj: { [P in K]?: string }, k: K) {
const key: K = k;
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}
}

namespace Problem5 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) {
while(!!true) {
obj[key].toUpperCase() // should Ok
}
}
}

namespace Problem6 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
while(!!true) {
if (obj[key]) {
obj[key].toUpperCase() // should Ok
}
}
}

namespace Problem7 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) {
while(!!true) {
obj[key].toUpperCase() // should error
~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
obj[key] = undefined
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//// [tests/cases/compiler/typeGuardNarrowsIndexedAccessOfAnyProperty.ts] ////

//// [typeGuardNarrowsIndexedAccessOfAnyProperty.ts]
namespace Problem1 {
declare const obj: { [key: string]: string | undefined };
declare let key: "a";
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem2 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem3 {
declare const obj: { a?: string, b?: string };
declare const key: "a" | "b";
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}

namespace Problem4 {
function f<K extends string>(obj: { [P in K]?: string }, k: K) {
const key: K = k;
if (obj[key]) { obj[key].toUpperCase() } // should Ok
}
}

namespace Problem5 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) {
while(!!true) {
obj[key].toUpperCase() // should Ok
}
}
}

namespace Problem6 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
while(!!true) {
if (obj[key]) {
obj[key].toUpperCase() // should Ok
}
}
}

namespace Problem7 {
declare const obj: { [key: string]: string | undefined };
declare const key: string;
if (obj[key]) {
while(!!true) {
obj[key].toUpperCase() // should error
obj[key] = undefined
}
}
}


//// [typeGuardNarrowsIndexedAccessOfAnyProperty.js]
"use strict";
var Problem1;
(function (Problem1) {
if (obj[key]) {
obj[key].toUpperCase();
} // should Ok
})(Problem1 || (Problem1 = {}));
var Problem2;
(function (Problem2) {
if (obj[key]) {
obj[key].toUpperCase();
} // should Ok
})(Problem2 || (Problem2 = {}));
var Problem3;
(function (Problem3) {
if (obj[key]) {
obj[key].toUpperCase();
} // should Ok
})(Problem3 || (Problem3 = {}));
var Problem4;
(function (Problem4) {
function f(obj, k) {
var key = k;
if (obj[key]) {
obj[key].toUpperCase();
} // should Ok
}
})(Problem4 || (Problem4 = {}));
var Problem5;
(function (Problem5) {
if (obj[key]) {
while (!!true) {
obj[key].toUpperCase(); // should Ok
}
}
})(Problem5 || (Problem5 = {}));
var Problem6;
(function (Problem6) {
while (!!true) {
if (obj[key]) {
obj[key].toUpperCase(); // should Ok
}
}
})(Problem6 || (Problem6 = {}));
var Problem7;
(function (Problem7) {
if (obj[key]) {
while (!!true) {
obj[key].toUpperCase(); // should error
obj[key] = undefined;
}
}
})(Problem7 || (Problem7 = {}));
Loading