Skip to content

Commit 6e01b06

Browse files
authored
Use const context in intersections containing const type variables (#55779)
1 parent 79736ef commit 6e01b06

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13912,7 +13912,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1391213912
function isConstTypeVariable(type: Type | undefined, depth = 0): boolean {
1391313913
return depth < 5 && !!(type && (
1391413914
type.flags & TypeFlags.TypeParameter && some((type as TypeParameter).symbol?.declarations, d => hasSyntacticModifier(d, ModifierFlags.Const)) ||
13915-
type.flags & TypeFlags.Union && some((type as UnionType).types, t => isConstTypeVariable(t, depth)) ||
13915+
type.flags & TypeFlags.UnionOrIntersection && some((type as UnionOrIntersectionType).types, t => isConstTypeVariable(t, depth)) ||
1391613916
type.flags & TypeFlags.IndexedAccess && isConstTypeVariable((type as IndexedAccessType).objectType, depth + 1) ||
1391713917
type.flags & TypeFlags.Conditional && isConstTypeVariable(getConstraintOfConditionalType(type as ConditionalType), depth + 1) ||
1391813918
type.flags & TypeFlags.Substitution && isConstTypeVariable((type as SubstitutionType).baseType, depth) ||
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiersWithIntersection.ts] ////
2+
3+
=== typeParameterConstModifiersWithIntersection.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55778
5+
6+
interface Config<T1 extends { type: string }> {
7+
>Config : Symbol(Config, Decl(typeParameterConstModifiersWithIntersection.ts, 0, 0))
8+
>T1 : Symbol(T1, Decl(typeParameterConstModifiersWithIntersection.ts, 2, 17))
9+
>type : Symbol(type, Decl(typeParameterConstModifiersWithIntersection.ts, 2, 29))
10+
11+
useIt: T1;
12+
>useIt : Symbol(Config.useIt, Decl(typeParameterConstModifiersWithIntersection.ts, 2, 47))
13+
>T1 : Symbol(T1, Decl(typeParameterConstModifiersWithIntersection.ts, 2, 17))
14+
}
15+
16+
declare function test<
17+
>test : Symbol(test, Decl(typeParameterConstModifiersWithIntersection.ts, 4, 1))
18+
19+
T1 extends { type: string },
20+
>T1 : Symbol(T1, Decl(typeParameterConstModifiersWithIntersection.ts, 6, 22))
21+
>type : Symbol(type, Decl(typeParameterConstModifiersWithIntersection.ts, 7, 14))
22+
23+
const TConfig extends Config<T1>,
24+
>TConfig : Symbol(TConfig, Decl(typeParameterConstModifiersWithIntersection.ts, 7, 30))
25+
>Config : Symbol(Config, Decl(typeParameterConstModifiersWithIntersection.ts, 0, 0))
26+
>T1 : Symbol(T1, Decl(typeParameterConstModifiersWithIntersection.ts, 6, 22))
27+
28+
>(config: { produceThing: T1 } & TConfig): TConfig;
29+
>config : Symbol(config, Decl(typeParameterConstModifiersWithIntersection.ts, 9, 2))
30+
>produceThing : Symbol(produceThing, Decl(typeParameterConstModifiersWithIntersection.ts, 9, 11))
31+
>T1 : Symbol(T1, Decl(typeParameterConstModifiersWithIntersection.ts, 6, 22))
32+
>TConfig : Symbol(TConfig, Decl(typeParameterConstModifiersWithIntersection.ts, 7, 30))
33+
>TConfig : Symbol(TConfig, Decl(typeParameterConstModifiersWithIntersection.ts, 7, 30))
34+
35+
const result = test({
36+
>result : Symbol(result, Decl(typeParameterConstModifiersWithIntersection.ts, 11, 5))
37+
>test : Symbol(test, Decl(typeParameterConstModifiersWithIntersection.ts, 4, 1))
38+
39+
produceThing: {} as {
40+
>produceThing : Symbol(produceThing, Decl(typeParameterConstModifiersWithIntersection.ts, 11, 21))
41+
42+
type: "foo";
43+
>type : Symbol(type, Decl(typeParameterConstModifiersWithIntersection.ts, 12, 23))
44+
45+
},
46+
useIt: {
47+
>useIt : Symbol(useIt, Decl(typeParameterConstModifiersWithIntersection.ts, 14, 4))
48+
49+
type: "foo",
50+
>type : Symbol(type, Decl(typeParameterConstModifiersWithIntersection.ts, 15, 10))
51+
52+
},
53+
extra: 10,
54+
>extra : Symbol(extra, Decl(typeParameterConstModifiersWithIntersection.ts, 17, 4))
55+
56+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiersWithIntersection.ts] ////
2+
3+
=== typeParameterConstModifiersWithIntersection.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55778
5+
6+
interface Config<T1 extends { type: string }> {
7+
>type : string
8+
9+
useIt: T1;
10+
>useIt : T1
11+
}
12+
13+
declare function test<
14+
>test : <T1 extends { type: string; }, const TConfig extends Config<T1>>(config: { produceThing: T1;} & TConfig) => TConfig
15+
16+
T1 extends { type: string },
17+
>type : string
18+
19+
const TConfig extends Config<T1>,
20+
>(config: { produceThing: T1 } & TConfig): TConfig;
21+
>config : { produceThing: T1; } & TConfig
22+
>produceThing : T1
23+
24+
const result = test({
25+
>result : { readonly produceThing: { type: "foo"; }; readonly useIt: { readonly type: "foo"; }; readonly extra: 10; }
26+
>test({ produceThing: {} as { type: "foo"; }, useIt: { type: "foo", }, extra: 10,}) : { readonly produceThing: { type: "foo"; }; readonly useIt: { readonly type: "foo"; }; readonly extra: 10; }
27+
>test : <T1 extends { type: string; }, const TConfig extends Config<T1>>(config: { produceThing: T1; } & TConfig) => TConfig
28+
>{ produceThing: {} as { type: "foo"; }, useIt: { type: "foo", }, extra: 10,} : { produceThing: { type: "foo"; }; useIt: { type: "foo"; }; extra: 10; }
29+
30+
produceThing: {} as {
31+
>produceThing : { type: "foo"; }
32+
>{} as { type: "foo"; } : { type: "foo"; }
33+
>{} : {}
34+
35+
type: "foo";
36+
>type : "foo"
37+
38+
},
39+
useIt: {
40+
>useIt : { type: "foo"; }
41+
>{ type: "foo", } : { type: "foo"; }
42+
43+
type: "foo",
44+
>type : "foo"
45+
>"foo" : "foo"
46+
47+
},
48+
extra: 10,
49+
>extra : 10
50+
>10 : 10
51+
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/55778
5+
6+
interface Config<T1 extends { type: string }> {
7+
useIt: T1;
8+
}
9+
10+
declare function test<
11+
T1 extends { type: string },
12+
const TConfig extends Config<T1>,
13+
>(config: { produceThing: T1 } & TConfig): TConfig;
14+
15+
const result = test({
16+
produceThing: {} as {
17+
type: "foo";
18+
},
19+
useIt: {
20+
type: "foo",
21+
},
22+
extra: 10,
23+
});

0 commit comments

Comments
 (0)