diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 41966df6672e9..b07a0a075080e 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -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); + 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: diff --git a/tests/baselines/reference/narrowingTypeofParenthesized1.symbols b/tests/baselines/reference/narrowingTypeofParenthesized1.symbols new file mode 100644 index 0000000000000..a24be2e1c7519 --- /dev/null +++ b/tests/baselines/reference/narrowingTypeofParenthesized1.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/narrowingTypeofParenthesized1.types b/tests/baselines/reference/narrowingTypeofParenthesized1.types new file mode 100644 index 0000000000000..423d7bfc23f2f --- /dev/null +++ b/tests/baselines/reference/narrowingTypeofParenthesized1.types @@ -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 +> : ^^^^^ +} + diff --git a/tests/cases/compiler/narrowingTypeofParenthesized1.ts b/tests/cases/compiler/narrowingTypeofParenthesized1.ts new file mode 100644 index 0000000000000..abdc5b4bc6851 --- /dev/null +++ b/tests/cases/compiler/narrowingTypeofParenthesized1.ts @@ -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; +}