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

Narrow types by typeof operands with extra parenthesis #60928

Merged
merged 1 commit into from
Jan 7, 2025
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
8 changes: 5 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,9 +1318,11 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
return isNarrowableOperand(expr.left) || isNarrowableOperand(expr.right) ||
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right) ||
(isBooleanLiteral(expr.right) && isNarrowingExpression(expr.left) || isBooleanLiteral(expr.left) && isNarrowingExpression(expr.right));
const left = skipParentheses(expr.left);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rechecked all of the branches in the containing function. They should all now look through parenthesis - this was the only branch missing this

const right = skipParentheses(expr.right);
return isNarrowableOperand(left) || isNarrowableOperand(right) ||
isNarrowingTypeofOperands(right, left) || isNarrowingTypeofOperands(left, right) ||
(isBooleanLiteral(right) && isNarrowingExpression(left) || isBooleanLiteral(left) && isNarrowingExpression(right));
case SyntaxKind.InstanceOfKeyword:
return isNarrowableOperand(expr.left);
case SyntaxKind.InKeyword:
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/narrowingTypeofParenthesized1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/narrowingTypeofParenthesized1.ts] ////

=== narrowingTypeofParenthesized1.ts ===
// https://github.com/microsoft/TypeScript/issues/42203

declare const foo: string;
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))

if ((typeof foo) === "string") {
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))

foo;
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))

} else {
foo;
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
}

if (typeof foo === ("string")) {
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))

foo;
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))

} else {
foo;
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
}

53 changes: 53 additions & 0 deletions tests/baselines/reference/narrowingTypeofParenthesized1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/compiler/narrowingTypeofParenthesized1.ts] ////

=== narrowingTypeofParenthesized1.ts ===
// https://github.com/microsoft/TypeScript/issues/42203

declare const foo: string;
>foo : string
> : ^^^^^^

if ((typeof foo) === "string") {
>(typeof foo) === "string" : boolean
> : ^^^^^^^
>(typeof foo) : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>typeof foo : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo : string
> : ^^^^^^
>"string" : "string"
> : ^^^^^^^^

foo;
>foo : string
> : ^^^^^^

} else {
foo;
>foo : never
> : ^^^^^
}

if (typeof foo === ("string")) {
>typeof foo === ("string") : boolean
> : ^^^^^^^
>typeof foo : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo : string
> : ^^^^^^
>("string") : "string"
> : ^^^^^^^^
>"string" : "string"
> : ^^^^^^^^

foo;
>foo : string
> : ^^^^^^

} else {
foo;
>foo : never
> : ^^^^^
}

18 changes: 18 additions & 0 deletions tests/cases/compiler/narrowingTypeofParenthesized1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/42203

declare const foo: string;

if ((typeof foo) === "string") {
foo;
} else {
foo;
}

if (typeof foo === ("string")) {
foo;
} else {
foo;
}
Loading