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

fix(50340): typeof ... === "undefined" check on discriminated union of undefined and object type doesn't narrow correctly #50344

Merged
merged 3 commits into from Aug 31, 2022
Merged
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
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -25260,11 +25260,19 @@ namespace ts {
}
const target = getReferenceCandidate(typeOfExpr.expression);
if (!isMatchingReference(reference, target)) {
const propertyAccess = getDiscriminantPropertyAccess(typeOfExpr.expression, type);
if (propertyAccess) {
return narrowTypeByDiscriminant(type, propertyAccess, t => narrowTypeByLiteralExpression(t, literal, assumeTrue));
}
if (strictNullChecks && optionalChainContainsReference(target, reference) && assumeTrue === (literal.text !== "undefined")) {
return getAdjustedTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
}
return type;
}
return narrowTypeByLiteralExpression(type, literal, assumeTrue);
}

function narrowTypeByLiteralExpression(type: Type, literal: LiteralExpression, assumeTrue: boolean) {
return assumeTrue ?
narrowTypeByTypeName(type, literal.text) :
getTypeWithFacts(type, typeofNEFacts.get(literal.text) || TypeFacts.TypeofNEHostObject);
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/narrowingTypeofUndefined.js
@@ -0,0 +1,12 @@
//// [narrowingTypeofUndefined.ts]
declare const a: { error: { a: string }, result: undefined } | { error: undefined, result: { b: number } }

if (typeof a.error === 'undefined') {
a.result.b; // ok
}


//// [narrowingTypeofUndefined.js]
if (typeof a.error === 'undefined') {
a.result.b; // ok
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/narrowingTypeofUndefined.symbols
@@ -0,0 +1,23 @@
=== tests/cases/compiler/narrowingTypeofUndefined.ts ===
declare const a: { error: { a: string }, result: undefined } | { error: undefined, result: { b: number } }
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 27))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 40))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 64))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 82))
>b : Symbol(b, Decl(narrowingTypeofUndefined.ts, 0, 92))

if (typeof a.error === 'undefined') {
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 64))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 64))

a.result.b; // ok
>a.result.b : Symbol(b, Decl(narrowingTypeofUndefined.ts, 0, 92))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 40), Decl(narrowingTypeofUndefined.ts, 0, 82))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 40), Decl(narrowingTypeofUndefined.ts, 0, 82))
>b : Symbol(b, Decl(narrowingTypeofUndefined.ts, 0, 92))
}

26 changes: 26 additions & 0 deletions tests/baselines/reference/narrowingTypeofUndefined.types
@@ -0,0 +1,26 @@
=== tests/cases/compiler/narrowingTypeofUndefined.ts ===
declare const a: { error: { a: string }, result: undefined } | { error: undefined, result: { b: number } }
>a : { error: { a: string;}; result: undefined; } | { error: undefined; result: { b: number;}; }
>error : { a: string; }
>a : string
>result : undefined
>error : undefined
>result : { b: number; }
>b : number

if (typeof a.error === 'undefined') {
>typeof a.error === 'undefined' : boolean
>typeof a.error : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a.error : { a: string; }
>a : { error: { a: string; }; result: undefined; } | { error: undefined; result: { b: number; }; }
>error : { a: string; }
>'undefined' : "undefined"

a.result.b; // ok
>a.result.b : number
>a.result : { b: number; }
>a : { error: { a: string; }; result: undefined; } | { error: undefined; result: { b: number; }; }
>result : { b: number; }
>b : number
}

5 changes: 5 additions & 0 deletions tests/cases/compiler/narrowingTypeofUndefined.ts
@@ -0,0 +1,5 @@
declare const a: { error: { a: string }, result: undefined } | { error: undefined, result: { b: number } }

if (typeof a.error === 'undefined') {
a.result.b; // ok
}
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved