-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathmappedTypeRemappingModifierMerging.js
More file actions
94 lines (79 loc) · 2.65 KB
/
mappedTypeRemappingModifierMerging.js
File metadata and controls
94 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//// [tests/cases/compiler/mappedTypeRemappingModifierMerging.ts] ////
//// [mappedTypeRemappingModifierMerging.ts]
// Mapped types with key remapping should merge modifiers consistently
// when multiple keys map to the same output key
type RemapKeyToInitialPart<T> = {
[K in keyof T as K extends `${infer First}.${infer _Rest}` ? First : K]: null;
};
// Both should produce { foo?: null } since at least one input is optional
type FirstOptional = RemapKeyToInitialPart<{
"foo.bar"?: string;
"foo.baz": number;
}>;
type FirstRequired = RemapKeyToInitialPart<{
"foo.baz": number;
"foo.bar"?: string;
}>;
// Test that they are equivalent
const testOptional: FirstOptional = { foo: null };
const testOptional2: FirstOptional = {};
const testRequired: FirstRequired = { foo: null };
const testRequired2: FirstRequired = {};
// Readonly should work the same way
type RemapWithReadonly<T> = {
[K in keyof T as K extends `${infer First}.${string}` ? First : K]: null;
};
type FirstReadonly = RemapWithReadonly<{
readonly "foo.bar": string;
"foo.baz": number;
}>;
type SecondReadonly = RemapWithReadonly<{
"foo.baz": number;
readonly "foo.bar": string;
}>;
declare const ro1: FirstReadonly;
declare const ro2: SecondReadonly;
// Both should be readonly
ro1.foo = null; // Error
ro2.foo = null; // Error
//// [mappedTypeRemappingModifierMerging.js]
"use strict";
// Mapped types with key remapping should merge modifiers consistently
// when multiple keys map to the same output key
// Test that they are equivalent
var testOptional = { foo: null };
var testOptional2 = {};
var testRequired = { foo: null };
var testRequired2 = {};
// Both should be readonly
ro1.foo = null; // Error
ro2.foo = null; // Error
//// [mappedTypeRemappingModifierMerging.d.ts]
type RemapKeyToInitialPart<T> = {
[K in keyof T as K extends `${infer First}.${infer _Rest}` ? First : K]: null;
};
type FirstOptional = RemapKeyToInitialPart<{
"foo.bar"?: string;
"foo.baz": number;
}>;
type FirstRequired = RemapKeyToInitialPart<{
"foo.baz": number;
"foo.bar"?: string;
}>;
declare const testOptional: FirstOptional;
declare const testOptional2: FirstOptional;
declare const testRequired: FirstRequired;
declare const testRequired2: FirstRequired;
type RemapWithReadonly<T> = {
[K in keyof T as K extends `${infer First}.${string}` ? First : K]: null;
};
type FirstReadonly = RemapWithReadonly<{
readonly "foo.bar": string;
"foo.baz": number;
}>;
type SecondReadonly = RemapWithReadonly<{
"foo.baz": number;
readonly "foo.bar": string;
}>;
declare const ro1: FirstReadonly;
declare const ro2: SecondReadonly;