Skip to content

Commit 3cf708e

Browse files
authored
Fixed types of properties of contextual filtering mapped types (#56201)
1 parent 9987812 commit 3cf708e

File tree

4 files changed

+251
-1
lines changed

4 files changed

+251
-1
lines changed

src/compiler/checker.ts

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

3162131621
function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) {
3162231622
return mapType(type, t => {
31623-
if (isGenericMappedType(t) && !t.declaration.nameType) {
31623+
if (isGenericMappedType(t) && getMappedTypeNameTypeKind(t) !== MappedTypeNameTypeKind.Remapping) {
3162431624
const constraint = getConstraintTypeFromMappedType(t);
3162531625
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
3162631626
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//// [tests/cases/compiler/contextualPropertyOfGenericFilteringMappedType.ts] ////
2+
3+
=== contextualPropertyOfGenericFilteringMappedType.ts ===
4+
declare function f1<T extends object>(
5+
>f1 : Symbol(f1, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 0))
6+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20))
7+
8+
data: T,
9+
>data : Symbol(data, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 38))
10+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20))
11+
12+
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void },
13+
>handlers : Symbol(handlers, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 1, 10))
14+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15))
15+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20))
16+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15))
17+
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 36))
18+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20))
19+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15))
20+
>prop : Symbol(prop, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 48))
21+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15))
22+
23+
): void;
24+
25+
f1(
26+
>f1 : Symbol(f1, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 0))
27+
{
28+
foo: 0,
29+
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 6, 3))
30+
31+
bar: "",
32+
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 7, 11))
33+
34+
},
35+
{
36+
foo: (value, key) => {},
37+
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 10, 3))
38+
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 10))
39+
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 16))
40+
41+
bar: (value, key) => {},
42+
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 28))
43+
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 12, 10))
44+
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 12, 16))
45+
46+
},
47+
);
48+
49+
declare function f2<T extends object>(
50+
>f2 : Symbol(f2, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 14, 2))
51+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20))
52+
53+
data: T,
54+
>data : Symbol(data, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 38))
55+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20))
56+
57+
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void },
58+
>handlers : Symbol(handlers, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 17, 10))
59+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15))
60+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20))
61+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20))
62+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15))
63+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15))
64+
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 66))
65+
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20))
66+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15))
67+
>prop : Symbol(prop, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 78))
68+
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15))
69+
70+
): void;
71+
72+
f2(
73+
>f2 : Symbol(f2, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 14, 2))
74+
{
75+
foo: 0,
76+
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 22, 3))
77+
78+
bar: "",
79+
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 23, 11))
80+
81+
},
82+
{
83+
bar: (value, key) => {},
84+
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 26, 3))
85+
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 27, 10))
86+
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 27, 16))
87+
88+
},
89+
);
90+
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//// [tests/cases/compiler/contextualPropertyOfGenericFilteringMappedType.ts] ////
2+
3+
=== contextualPropertyOfGenericFilteringMappedType.ts ===
4+
declare function f1<T extends object>(
5+
>f1 : <T extends object>(data: T, handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void; }) => void
6+
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
7+
8+
data: T,
9+
>data : T
10+
> : ^
11+
12+
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void },
13+
>handlers : { [P in keyof T as P]: (value: T[P], prop: P) => void; }
14+
> : ^^^ ^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^ ^^^
15+
>value : T[P]
16+
> : ^^^^
17+
>prop : P
18+
> : ^
19+
20+
): void;
21+
22+
f1(
23+
>f1( { foo: 0, bar: "", }, { foo: (value, key) => {}, bar: (value, key) => {}, },) : void
24+
> : ^^^^
25+
>f1 : <T extends object>(data: T, handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void; }) => void
26+
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
27+
{
28+
>{ foo: 0, bar: "", } : { foo: number; bar: string; }
29+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
foo: 0,
32+
>foo : number
33+
> : ^^^^^^
34+
>0 : 0
35+
> : ^
36+
37+
bar: "",
38+
>bar : string
39+
> : ^^^^^^
40+
>"" : ""
41+
> : ^^
42+
43+
},
44+
{
45+
>{ foo: (value, key) => {}, bar: (value, key) => {}, } : { foo: (value: number, key: "foo") => void; bar: (value: string, key: "bar") => void; }
46+
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
47+
48+
foo: (value, key) => {},
49+
>foo : (value: number, key: "foo") => void
50+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
51+
>(value, key) => {} : (value: number, key: "foo") => void
52+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
53+
>value : number
54+
> : ^^^^^^
55+
>key : "foo"
56+
> : ^^^^^
57+
58+
bar: (value, key) => {},
59+
>bar : (value: string, key: "bar") => void
60+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
61+
>(value, key) => {} : (value: string, key: "bar") => void
62+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
63+
>value : string
64+
> : ^^^^^^
65+
>key : "bar"
66+
> : ^^^^^
67+
68+
},
69+
);
70+
71+
declare function f2<T extends object>(
72+
>f2 : <T extends object>(data: T, handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; }) => void
73+
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
74+
75+
data: T,
76+
>data : T
77+
> : ^
78+
79+
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void },
80+
>handlers : { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; }
81+
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^ ^^^
82+
>value : T[P]
83+
> : ^^^^
84+
>prop : P
85+
> : ^
86+
87+
): void;
88+
89+
f2(
90+
>f2( { foo: 0, bar: "", }, { bar: (value, key) => {}, },) : void
91+
> : ^^^^
92+
>f2 : <T extends object>(data: T, handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; }) => void
93+
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
94+
{
95+
>{ foo: 0, bar: "", } : { foo: number; bar: string; }
96+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97+
98+
foo: 0,
99+
>foo : number
100+
> : ^^^^^^
101+
>0 : 0
102+
> : ^
103+
104+
bar: "",
105+
>bar : string
106+
> : ^^^^^^
107+
>"" : ""
108+
> : ^^
109+
110+
},
111+
{
112+
>{ bar: (value, key) => {}, } : { bar: (value: string, key: "bar") => void; }
113+
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
114+
115+
bar: (value, key) => {},
116+
>bar : (value: string, key: "bar") => void
117+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
118+
>(value, key) => {} : (value: string, key: "bar") => void
119+
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^
120+
>value : string
121+
> : ^^^^^^
122+
>key : "bar"
123+
> : ^^^^^
124+
125+
},
126+
);
127+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
declare function f1<T extends object>(
5+
data: T,
6+
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void },
7+
): void;
8+
9+
f1(
10+
{
11+
foo: 0,
12+
bar: "",
13+
},
14+
{
15+
foo: (value, key) => {},
16+
bar: (value, key) => {},
17+
},
18+
);
19+
20+
declare function f2<T extends object>(
21+
data: T,
22+
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void },
23+
): void;
24+
25+
f2(
26+
{
27+
foo: 0,
28+
bar: "",
29+
},
30+
{
31+
bar: (value, key) => {},
32+
},
33+
);

0 commit comments

Comments
 (0)