Skip to content
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

Improve checking of in operator combined with noUncheckedIndexedAccess #50936

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/compiler/checker.ts
Expand Up @@ -25251,17 +25251,33 @@ namespace ts {
if (isKnownProperty) {
// If the check is for a known property (i.e. a property declared in some constituent of
// the target type), we filter the target type by presence of absence of the property.
return filterType(type, t => isTypePresencePossible(t, name, assumeTrue));
const filtered = filterType(type, t => isTypePresencePossible(t, name, assumeTrue));
// without `noUncheckedIndexedAccess` we can't narrow any further
// `{ [x: PropertyKey]: T }[prop]` already returns `T`
// `{ prop?: T }[prop]` always implies `T | undefined`
if (!compilerOptions.noUncheckedIndexedAccess) {
return filtered;
}
return mapType(filtered, t => {
if (getPropertyOfType(type, name)) {
return t;
}
return intersectWithNarrowingRecordType(type, nameType, getApplicableIndexInfoForName(type, name)!.type);
});
Andarist marked this conversation as resolved.
Show resolved Hide resolved
}
if (assumeTrue) {
// If the check is for an unknown property, we intersect the target type with `Record<X, unknown>`,
// where X is the name of the property.
const recordSymbol = getGlobalRecordSymbol();
if (recordSymbol) {
return getIntersectionType([type, getTypeAliasInstantiation(recordSymbol, [nameType, unknownType])]);
}
return intersectWithNarrowingRecordType(type, nameType, unknownType);
}
return type;

function intersectWithNarrowingRecordType(type: Type, nameType: StringLiteralType | NumberLiteralType | UniqueESSymbolType, propType: Type) {
const recordSymbol = getGlobalRecordSymbol();
return recordSymbol
? getIntersectionType([type, getTypeAliasInstantiation(recordSymbol, [nameType, propType])])
: type;
}
}

function narrowTypeByBinaryExpression(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
Expand Down
@@ -0,0 +1,39 @@
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts ===
declare function invariant(condition: boolean): asserts condition;
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0))
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27))
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27))

function f1(obj: Record<string, string>) {
>f1 : Symbol(f1, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 66))
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

invariant("test" in obj);
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0))
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))

return obj.test
>obj.test : Symbol(test)
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))
>test : Symbol(test)
}

function f2(obj: Record<string, string>) {
>f2 : Symbol(f2, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 5, 1))
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

if ("test" in obj) {
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))

return obj.test
>obj.test : Symbol(test)
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))
>test : Symbol(test)
}
return "default"
}



@@ -0,0 +1,42 @@
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts ===
declare function invariant(condition: boolean): asserts condition;
>invariant : (condition: boolean) => asserts condition
>condition : boolean

function f1(obj: Record<string, string>) {
>f1 : (obj: Record<string, string>) => string
>obj : Record<string, string>

invariant("test" in obj);
>invariant("test" in obj) : void
>invariant : (condition: boolean) => asserts condition
>"test" in obj : boolean
>"test" : "test"
>obj : Record<string, string>

return obj.test
>obj.test : string
>obj : Record<string, string> & Record<"test", string>
>test : string
}

function f2(obj: Record<string, string>) {
>f2 : (obj: Record<string, string>) => string
>obj : Record<string, string>

if ("test" in obj) {
>"test" in obj : boolean
>"test" : "test"
>obj : Record<string, string>

return obj.test
>obj.test : string
>obj : Record<string, string> & Record<"test", string>
>test : string
}
return "default"
>"default" : "default"
}



@@ -0,0 +1,19 @@
// @strict: true
// @noEmit: true
// @noUncheckedIndexedAccess: true

declare function invariant(condition: boolean): asserts condition;

function f1(obj: Record<string, string>) {
invariant("test" in obj);
return obj.test
}

function f2(obj: Record<string, string>) {
if ("test" in obj) {
return obj.test
}
return "default"
}