Get narrowable types for new expressions (#57382)

This commit is contained in:
Mateusz Burzyński
2024-02-28 21:33:38 +01:00
committed by GitHub
parent f23927a806
commit 63dd17baef
5 changed files with 57 additions and 0 deletions

View File

@@ -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)));
}

View File

@@ -82,6 +82,14 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
obj[key];
}
}
// https://github.com/microsoft/TypeScript/issues/57381
function f6<T extends string | (new () => {})>(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();
}
}

View File

@@ -221,3 +221,19 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
}
}
// https://github.com/microsoft/TypeScript/issues/57381
function f6<T extends string | (new () => {})>(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))
}
}

View File

@@ -200,3 +200,21 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
}
}
// https://github.com/microsoft/TypeScript/issues/57381
function f6<T extends string | (new () => {})>(a: T) {
>f6 : <T extends string | (new () => {})>(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 () => {}
}
}

View File

@@ -81,3 +81,11 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
obj[key];
}
}
// https://github.com/microsoft/TypeScript/issues/57381
function f6<T extends string | (new () => {})>(a: T) {
if (typeof a !== "string") {
new a();
}
}