Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/baselines/reference/narrowingTypeofUndefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ if (typeof a.error !== 'undefined') {
else {
a.result.prop; // number
}

// extra parens on purpose
if (typeof (a.error) === 'undefined') {
a.result.prop; // number
}


//// [narrowingTypeofUndefined.js]
"use strict";
if (typeof a.error === 'undefined') {
a.result.prop; // number
}
Expand All @@ -29,3 +35,7 @@ if (typeof a.error !== 'undefined') {
else {
a.result.prop; // number
}
// extra parens on purpose
if (typeof (a.error) === 'undefined') {
a.result.prop; // number
}
30 changes: 22 additions & 8 deletions tests/baselines/reference/narrowingTypeofUndefined.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ if (typeof a.error === 'undefined') {

a.result.prop; // number
>a.result.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
}
else {
a.error.prop; // string
>a.error.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27))
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18))
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27))
}

Expand All @@ -36,17 +36,31 @@ if (typeof a.error !== 'undefined') {

a.error.prop; // string
>a.error.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27))
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18))
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 27))
}
else {
a.result.prop; // number
>a.result.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 43), Decl(narrowingTypeofUndefined.ts, 0, 85))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
}

// extra parens on purpose
if (typeof (a.error) === 'undefined') {
>a.error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>error : Symbol(error, Decl(narrowingTypeofUndefined.ts, 0, 18), Decl(narrowingTypeofUndefined.ts, 0, 67))

a.result.prop; // number
>a.result.prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
>a.result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>a : Symbol(a, Decl(narrowingTypeofUndefined.ts, 0, 13))
>result : Symbol(result, Decl(narrowingTypeofUndefined.ts, 0, 85))
>prop : Symbol(prop, Decl(narrowingTypeofUndefined.ts, 0, 95))
}

32 changes: 25 additions & 7 deletions tests/baselines/reference/narrowingTypeofUndefined.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,65 @@ declare const a: { error: { prop: string }, result: undefined } | { error: undef
if (typeof a.error === 'undefined') {
>typeof a.error === 'undefined' : boolean
>typeof a.error : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a.error : { prop: string; }
>a.error : { prop: string; } | undefined
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; }
>error : { prop: string; }
>error : { prop: string; } | undefined
>'undefined' : "undefined"

a.result.prop; // number
>a.result.prop : number
>a.result : { prop: number; }
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; }
>a : { error: undefined; result: { prop: number; }; }
>result : { prop: number; }
>prop : number
}
else {
a.error.prop; // string
>a.error.prop : string
>a.error : { prop: string; }
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; }
>a : { error: { prop: string; }; result: undefined; }
>error : { prop: string; }
>prop : string
}

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

a.error.prop; // string
>a.error.prop : string
>a.error : { prop: string; }
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; }
>a : { error: { prop: string; }; result: undefined; }
>error : { prop: string; }
>prop : string
}
else {
a.result.prop; // number
>a.result.prop : number
>a.result : { prop: number; }
>a : { error: undefined; result: { prop: number; }; }
>result : { prop: number; }
>prop : number
}

// extra parens on purpose
if (typeof (a.error) === 'undefined') {
>typeof (a.error) === 'undefined' : boolean
>typeof (a.error) : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>(a.error) : { prop: string; } | undefined
>a.error : { prop: string; } | undefined
>a : { error: { prop: string; }; result: undefined; } | { error: undefined; result: { prop: number; }; }
>error : { prop: string; } | undefined
>'undefined' : "undefined"

a.result.prop; // number
>a.result.prop : number
>a.result : { prop: number; }
>a : { error: undefined; result: { prop: number; }; }
>result : { prop: number; }
>prop : number
}
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/compiler/narrowingTypeofUndefined.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @strict: true

declare const a: { error: { prop: string }, result: undefined } | { error: undefined, result: { prop: number } }

if (typeof a.error === 'undefined') {
Expand All @@ -13,3 +15,8 @@ if (typeof a.error !== 'undefined') {
else {
a.result.prop; // number
}

// extra parens on purpose
if (typeof (a.error) === 'undefined') {
a.result.prop; // number
}
Comment on lines +19 to +
Copy link
Copy Markdown
Contributor Author

@Andarist Andarist Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was an intended fix. I'm not sure we need this extra test since #51270 already includes a regression test from #51700 with two typeof checks that differ only in parentheses. It was in fact that difference that caused me to fix the issue.

Copy link
Copy Markdown
Contributor Author

@Andarist Andarist Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#51720 has a test with optional chaining and parenthesis - that has worked before your PR despite the fact that the version without parenthesis reported an error. This test is kinda about the opposite situation (no optional chaining and parenthesis) - the version without parenthesis worked alright while the version with them reported an error.

I've concluded that they are slightly different and thus I've created a PR (I already had it locally as I was looking into fixing #51700 when you picked it up). I understand that they can be seen as similar enough though. Feel free to merge or close - whatever you think is best here.