diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2f3de0bf2da..80477543f44 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -29071,6 +29071,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return parent.kind === SyntaxKind.PropertyAccessExpression || parent.kind === SyntaxKind.QualifiedName || parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === node || + parent.kind === SyntaxKind.NewExpression && (parent as NewExpression).expression === node || parent.kind === SyntaxKind.ElementAccessExpression && (parent as ElementAccessExpression).expression === node && !(someType(type, isGenericTypeWithoutNullableConstraint) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression))); } diff --git a/tests/baselines/reference/typeVariableTypeGuards.js b/tests/baselines/reference/typeVariableTypeGuards.js index 980f176e2c3..20db164b2d3 100644 --- a/tests/baselines/reference/typeVariableTypeGuards.js +++ b/tests/baselines/reference/typeVariableTypeGuards.js @@ -82,6 +82,14 @@ function f5(obj: T | undefined, key: K) { obj[key]; } } + +// https://github.com/microsoft/TypeScript/issues/57381 + +function f6 {})>(a: T) { + if (typeof a !== "string") { + new a(); + } +} //// [typeVariableTypeGuards.js] @@ -165,3 +173,9 @@ function f5(obj, key) { obj[key]; } } +// https://github.com/microsoft/TypeScript/issues/57381 +function f6(a) { + if (typeof a !== "string") { + new a(); + } +} diff --git a/tests/baselines/reference/typeVariableTypeGuards.symbols b/tests/baselines/reference/typeVariableTypeGuards.symbols index 73e8a58f2ec..4b847c14417 100644 --- a/tests/baselines/reference/typeVariableTypeGuards.symbols +++ b/tests/baselines/reference/typeVariableTypeGuards.symbols @@ -221,3 +221,19 @@ function f5(obj: T | undefined, key: K) { } } +// https://github.com/microsoft/TypeScript/issues/57381 + +function f6 {})>(a: T) { +>f6 : Symbol(f6, Decl(typeVariableTypeGuards.ts, 80, 1)) +>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 84, 12)) +>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47)) +>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 84, 12)) + + if (typeof a !== "string") { +>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47)) + + new a(); +>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47)) + } +} + diff --git a/tests/baselines/reference/typeVariableTypeGuards.types b/tests/baselines/reference/typeVariableTypeGuards.types index ddad0ef69ac..10cfba2409e 100644 --- a/tests/baselines/reference/typeVariableTypeGuards.types +++ b/tests/baselines/reference/typeVariableTypeGuards.types @@ -200,3 +200,21 @@ function f5(obj: T | undefined, key: K) { } } +// https://github.com/microsoft/TypeScript/issues/57381 + +function f6 {})>(a: T) { +>f6 : {})>(a: T) => void +>a : T + + if (typeof a !== "string") { +>typeof a !== "string" : boolean +>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>a : T +>"string" : "string" + + new a(); +>new a() : {} +>a : new () => {} + } +} + diff --git a/tests/cases/compiler/typeVariableTypeGuards.ts b/tests/cases/compiler/typeVariableTypeGuards.ts index 91e5e7a552a..4042132708e 100644 --- a/tests/cases/compiler/typeVariableTypeGuards.ts +++ b/tests/cases/compiler/typeVariableTypeGuards.ts @@ -81,3 +81,11 @@ function f5(obj: T | undefined, key: K) { obj[key]; } } + +// https://github.com/microsoft/TypeScript/issues/57381 + +function f6 {})>(a: T) { + if (typeof a !== "string") { + new a(); + } +}