Skip to content

Commit 63dd17b

Browse files
authored
Get narrowable types for new expressions (microsoft#57382)
1 parent f23927a commit 63dd17b

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29071,6 +29071,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2907129071
return parent.kind === SyntaxKind.PropertyAccessExpression ||
2907229072
parent.kind === SyntaxKind.QualifiedName ||
2907329073
parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === node ||
29074+
parent.kind === SyntaxKind.NewExpression && (parent as NewExpression).expression === node ||
2907429075
parent.kind === SyntaxKind.ElementAccessExpression && (parent as ElementAccessExpression).expression === node &&
2907529076
!(someType(type, isGenericTypeWithoutNullableConstraint) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
2907629077
}

tests/baselines/reference/typeVariableTypeGuards.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
8282
obj[key];
8383
}
8484
}
85+
86+
// https://github.com/microsoft/TypeScript/issues/57381
87+
88+
function f6<T extends string | (new () => {})>(a: T) {
89+
if (typeof a !== "string") {
90+
new a();
91+
}
92+
}
8593

8694

8795
//// [typeVariableTypeGuards.js]
@@ -165,3 +173,9 @@ function f5(obj, key) {
165173
obj[key];
166174
}
167175
}
176+
// https://github.com/microsoft/TypeScript/issues/57381
177+
function f6(a) {
178+
if (typeof a !== "string") {
179+
new a();
180+
}
181+
}

tests/baselines/reference/typeVariableTypeGuards.symbols

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,19 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
221221
}
222222
}
223223

224+
// https://github.com/microsoft/TypeScript/issues/57381
225+
226+
function f6<T extends string | (new () => {})>(a: T) {
227+
>f6 : Symbol(f6, Decl(typeVariableTypeGuards.ts, 80, 1))
228+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 84, 12))
229+
>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47))
230+
>T : Symbol(T, Decl(typeVariableTypeGuards.ts, 84, 12))
231+
232+
if (typeof a !== "string") {
233+
>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47))
234+
235+
new a();
236+
>a : Symbol(a, Decl(typeVariableTypeGuards.ts, 84, 47))
237+
}
238+
}
239+

tests/baselines/reference/typeVariableTypeGuards.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,21 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
200200
}
201201
}
202202

203+
// https://github.com/microsoft/TypeScript/issues/57381
204+
205+
function f6<T extends string | (new () => {})>(a: T) {
206+
>f6 : <T extends string | (new () => {})>(a: T) => void
207+
>a : T
208+
209+
if (typeof a !== "string") {
210+
>typeof a !== "string" : boolean
211+
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
212+
>a : T
213+
>"string" : "string"
214+
215+
new a();
216+
>new a() : {}
217+
>a : new () => {}
218+
}
219+
}
220+

tests/cases/compiler/typeVariableTypeGuards.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
8181
obj[key];
8282
}
8383
}
84+
85+
// https://github.com/microsoft/TypeScript/issues/57381
86+
87+
function f6<T extends string | (new () => {})>(a: T) {
88+
if (typeof a !== "string") {
89+
new a();
90+
}
91+
}

0 commit comments

Comments
 (0)