Skip to content

Commit 41ebfbf

Browse files
authored
Fixed an issue with string mappings over generic intersections not being deferred in conditional types (#55856)
1 parent aa9b695 commit 41ebfbf

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18008,7 +18008,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1800818008

1800918009
function isPatternLiteralPlaceholderType(type: Type): boolean {
1801018010
if (type.flags & TypeFlags.Intersection) {
18011-
return some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Null | TypeFlags.Undefined)) || isPatternLiteralPlaceholderType(t));
18011+
return !isGenericType(type) && some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Nullable)) || isPatternLiteralPlaceholderType(t));
1801218012
}
1801318013
return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(type);
1801418014
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] ////
2+
3+
=== stringMappingDeferralInConditionalTypes.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55847
5+
6+
type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
7+
>A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0))
8+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7))
9+
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
10+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7))
11+
12+
let x1: A<"foo"> = 1; // ok
13+
>x1 : Symbol(x1, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 3))
14+
>A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0))
15+
16+
type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
17+
>B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21))
18+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7))
19+
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
20+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7))
21+
22+
let x2: B<"foo"> = 1; // ok
23+
>x2 : Symbol(x2, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 3))
24+
>B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21))
25+
26+
type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
27+
>C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21))
28+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7))
29+
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --))
30+
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
31+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7))
32+
33+
let x3: C<"foo"> = 1; // ok
34+
>x3 : Symbol(x3, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 3))
35+
>C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21))
36+
37+
type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
38+
>D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21))
39+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7))
40+
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --))
41+
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
42+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7))
43+
44+
let x4: D<"foo"> = 1; // ok
45+
>x4 : Symbol(x4, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 3))
46+
>D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21))
47+
48+
type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
49+
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))
50+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))
51+
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
52+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))
53+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))
54+
55+
type F = E<""> extends "f" ? 1 : 0; // 1
56+
>F : Symbol(F, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 59))
57+
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))
58+
59+
type G<S> = E<S> extends "f" ? 1 : 0;
60+
>G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35))
61+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7))
62+
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))
63+
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7))
64+
65+
let x5: G<""> = 1; // ok
66+
>x5 : Symbol(x5, Decl(stringMappingDeferralInConditionalTypes.ts, 17, 3))
67+
>G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35))
68+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] ////
2+
3+
=== stringMappingDeferralInConditionalTypes.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55847
5+
6+
type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
7+
>A : A<S>
8+
9+
let x1: A<"foo"> = 1; // ok
10+
>x1 : 1
11+
>1 : 1
12+
13+
type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
14+
>B : B<S>
15+
16+
let x2: B<"foo"> = 1; // ok
17+
>x2 : 1
18+
>1 : 1
19+
20+
type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
21+
>C : C<S>
22+
23+
let x3: C<"foo"> = 1; // ok
24+
>x3 : 1
25+
>1 : 1
26+
27+
type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
28+
>D : D<S>
29+
30+
let x4: D<"foo"> = 1; // ok
31+
>x4 : 1
32+
>1 : 1
33+
34+
type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
35+
>E : Lowercase<`f${S & string}` & `${S & string}f`>
36+
37+
type F = E<""> extends "f" ? 1 : 0; // 1
38+
>F : 1
39+
40+
type G<S> = E<S> extends "f" ? 1 : 0;
41+
>G : G<S>
42+
43+
let x5: G<""> = 1; // ok
44+
>x5 : 1
45+
>1 : 1
46+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/55847
5+
6+
type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
7+
let x1: A<"foo"> = 1; // ok
8+
9+
type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
10+
let x2: B<"foo"> = 1; // ok
11+
12+
type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
13+
let x3: C<"foo"> = 1; // ok
14+
15+
type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
16+
let x4: D<"foo"> = 1; // ok
17+
18+
type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
19+
type F = E<""> extends "f" ? 1 : 0; // 1
20+
type G<S> = E<S> extends "f" ? 1 : 0;
21+
let x5: G<""> = 1; // ok

0 commit comments

Comments
 (0)