File: mappedTypeAsClauses.types

package info (click to toggle)
node-typescript 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 396,552 kB
  • sloc: javascript: 1,444,377; makefile: 7; sh: 3
file content (172 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
=== tests/cases/conformance/types/mapped/mappedTypeAsClauses.ts ===
// Mapped type 'as N' clauses

type Getters<T> = { [P in keyof T & string as `get${Capitalize<P>}`]: () => T[P] };
>Getters : Getters<T>

type TG1 = Getters<{ foo: string, bar: number, baz: { z: boolean } }>;
>TG1 : Getters<{ foo: string; bar: number; baz: {    z: boolean;}; }>
>foo : string
>bar : number
>baz : { z: boolean; }
>z : boolean

// Mapped type with 'as N' clause has no constraint on 'in T' clause

type PropDef<K extends keyof any, T> = { name: K, type: T };
>PropDef : PropDef<K, T>
>name : K
>type : T

type TypeFromDefs<T extends PropDef<keyof any, any>> = { [P in T as P['name']]: P['type'] };
>TypeFromDefs : TypeFromDefs<T>

type TP1 = TypeFromDefs<{ name: 'a', type: string } | { name: 'b', type: number } | { name: 'a', type: boolean }>;
>TP1 : TypeFromDefs<{ name: 'a'; type: string; } | { name: 'b'; type: number; } | { name: 'a'; type: boolean; }>
>name : "a"
>type : string
>name : "b"
>type : number
>name : "a"
>type : boolean

// No array or tuple type mapping when 'as N' clause present

type TA1 = Getters<string[]>;
>TA1 : Getters<string[]>

type TA2 = Getters<[number, boolean]>;
>TA2 : Getters<[number, boolean]>

// Filtering using 'as N' clause

type Methods<T> = { [P in keyof T as T[P] extends Function ? P : never]: T[P] };
>Methods : Methods<T>

type TM1 = Methods<{ foo(): number, bar(x: string): boolean, baz: string | number }>;
>TM1 : Methods<{ foo(): number; bar(x: string): boolean; baz: string | number; }>
>foo : () => number
>bar : (x: string) => boolean
>x : string
>baz : string | number

// Mapping to multiple names using 'as N' clause

type DoubleProp<T> = { [P in keyof T & string as `${P}1` | `${P}2`]: T[P] }
>DoubleProp : DoubleProp<T>

type TD1 = DoubleProp<{ a: string, b: number }>;  // { a1: string, a2: string, b1: number, b2: number }
>TD1 : DoubleProp<{ a: string; b: number; }>
>a : string
>b : number

type TD2 = keyof TD1;  // 'a1' | 'a2' | 'b1' | 'b2'
>TD2 : "a1" | "a2" | "b1" | "b2"

type TD3<U> = keyof DoubleProp<U>;  // `${keyof U & string}1` | `${keyof U & string}2`
>TD3 : `${keyof U & string}1` | `${keyof U & string}2`

// Repro from #40619

type Lazyify<T> = {
>Lazyify : Lazyify<T>

    [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K]
};

interface Person {
    readonly name: string;
>name : string

    age: number;
>age : number

    location?: string;
>location : string | undefined
}

type LazyPerson = Lazyify<Person>;
>LazyPerson : Lazyify<Person>

// Repro from #40833

type Example = {foo: string, bar: number};
>Example : Example
>foo : string
>bar : number

type PickByValueType<T, U> = {
>PickByValueType : PickByValueType<T, U>

  [K in keyof T as T[K] extends U ? K : never]: T[K]
};

type T1 = PickByValueType<Example, string>;
>T1 : PickByValueType<Example, string>

const e1: T1 = {
>e1 : PickByValueType<Example, string>
>{    foo: "hello"} : { foo: string; }

    foo: "hello"
>foo : string
>"hello" : "hello"

};
type T2 = keyof T1;
>T2 : "foo"

const e2: T2 = "foo";
>e2 : "foo"
>"foo" : "foo"

// Repro from #41133

interface Car {
    name: string;
>name : string

    seats: number;
>seats : number

    engine: Engine;
>engine : Engine

    wheels: Wheel[];
>wheels : Wheel[]
}

interface Engine {
    manufacturer: string;
>manufacturer : string

    horsepower: number;
>horsepower : number
}

interface Wheel {
    type: "summer" | "winter";
>type : "summer" | "winter"

    radius: number;
>radius : number
}

type Primitive = string | number | boolean;
>Primitive : Primitive

type OnlyPrimitives<T> = { [K in keyof T as T[K] extends Primitive ? K : never]: T[K] };
>OnlyPrimitives : OnlyPrimitives<T>

let primitiveCar: OnlyPrimitives<Car>;  // { name: string; seats: number; }
>primitiveCar : OnlyPrimitives<Car>

let keys: keyof OnlyPrimitives<Car>;  //  "name" | "seats"
>keys : "name" | "seats"

type KeysOfPrimitives<T> = keyof OnlyPrimitives<T>;
>KeysOfPrimitives : keyof OnlyPrimitives<T>

let carKeys: KeysOfPrimitives<Car>;  // "name" | "seats"
>carKeys : "name" | "seats"