File: excessPropertyCheckWithMultipleDiscriminants.types

package info (click to toggle)
node-typescript 5.1.6%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 492,516 kB
  • sloc: javascript: 2,078,951; makefile: 6; sh: 1
file content (310 lines) | stat: -rw-r--r-- 6,471 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
=== tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts ===
// Repro from #32657

interface Base<T> {
    value: T;
>value : T
}

interface Int extends Base<number> {
    type: "integer";
>type : "integer"

    multipleOf?: number;
>multipleOf : number
}

interface Float extends Base<number> {
    type: "number";
>type : "number"
}

interface Str extends Base<string> {
    type: "string";
>type : "string"

    format?: string;
>format : string
}

interface Bool extends Base<boolean> {
    type: "boolean";
>type : "boolean"
}

type Primitive = Int | Float | Str | Bool;
>Primitive : Int | Float | Str | Bool

const foo: Primitive = {
>foo : Primitive
>{    type: "number",    value: 10,    multipleOf: 5, // excess property    format: "what?"} : { type: "number"; value: number; multipleOf: number; format: string; }

    type: "number",
>type : "number"
>"number" : "number"

    value: 10,
>value : number
>10 : 10

    multipleOf: 5, // excess property
>multipleOf : number
>5 : 5

    format: "what?"
>format : string
>"what?" : "what?"
}


type DisjointDiscriminants = { p1: 'left'; p2: true; p3: number } | { p1: 'right'; p2: false; p4: string } | { p1: 'left'; p2: boolean };
>DisjointDiscriminants : { p1: 'left'; p2: true; p3: number; } | { p1: 'right'; p2: false; p4: string; } | { p1: 'left'; p2: boolean; }
>p1 : "left"
>p2 : true
>true : true
>p3 : number
>p1 : "right"
>p2 : false
>false : false
>p4 : string
>p1 : "left"
>p2 : boolean

// This has excess error because variant three is the only applicable case.
const a: DisjointDiscriminants = {
>a : DisjointDiscriminants
>{    p1: 'left',    p2: false,    p3: 42,    p4: "hello"} : { p1: "left"; p2: false; p3: number; p4: string; }

    p1: 'left',
>p1 : "left"
>'left' : "left"

    p2: false,
>p2 : false
>false : false

    p3: 42,
>p3 : number
>42 : 42

    p4: "hello"
>p4 : string
>"hello" : "hello"

};

// This has excess error because variant two is not applicable.
const b: DisjointDiscriminants = {
>b : DisjointDiscriminants
>{    p1: 'left',    p2: true,    p3: 42,    p4: "hello"} : { p1: "left"; p2: true; p3: number; p4: string; }

    p1: 'left',
>p1 : "left"
>'left' : "left"

    p2: true,
>p2 : true
>true : true

    p3: 42,
>p3 : number
>42 : 42

    p4: "hello"
>p4 : string
>"hello" : "hello"

};

// This has excess error because variant two is the only applicable case
const c: DisjointDiscriminants = {
>c : DisjointDiscriminants
>{    p1: 'right',    p2: false,    p3: 42,    p4: "hello"} : { p1: "right"; p2: false; p3: number; p4: string; }

    p1: 'right',
>p1 : "right"
>'right' : "right"

    p2: false,
>p2 : false
>false : false

    p3: 42,
>p3 : number
>42 : 42

    p4: "hello"
>p4 : string
>"hello" : "hello"

};

// Repro from #51873

interface Common {
    type: "A" | "B" | "C" | "D";
>type : "A" | "B" | "C" | "D"

    n: number;
>n : number
}
interface A {
    type: "A";
>type : "A"

    a?: number;
>a : number
}
interface B {
    type: "B";
>type : "B"

    b?: number;
>b : number
}

type CommonWithOverlappingOptionals = Common | (Common & A) | (Common & B);
>CommonWithOverlappingOptionals : Common | (Common & A) | (Common & B)

// Should reject { b } because reduced to Common | (Common & A)
const c1: CommonWithOverlappingOptionals = {
>c1 : CommonWithOverlappingOptionals
>{    type: "A",    n: 1,    a: 1,    b: 1  // excess property} : { type: "A"; n: number; a: number; b: number; }

    type: "A",
>type : "A"
>"A" : "A"

    n: 1,
>n : number
>1 : 1

    a: 1,
>a : number
>1 : 1

    b: 1  // excess property
>b : number
>1 : 1
}

type CommonWithDisjointOverlappingOptionals = Common | A | B;
>CommonWithDisjointOverlappingOptionals : Common | A | B

// Should still reject { b } because reduced to Common | A, even though these are now disjoint
const c2: CommonWithDisjointOverlappingOptionals = {
>c2 : CommonWithDisjointOverlappingOptionals
>{    type: "A",    n: 1,    a: 1,    b: 1  // excess property} : { type: "A"; n: number; a: number; b: number; }

    type: "A",
>type : "A"
>"A" : "A"

    n: 1,
>n : number
>1 : 1

    a: 1,
>a : number
>1 : 1

    b: 1  // excess property
>b : number
>1 : 1
}

// Repro from https://github.com/microsoft/TypeScript/pull/51884#issuecomment-1472736068

export type BaseAttribute<T> = {
>BaseAttribute : BaseAttribute<T>

    type?: string | undefined;
>type : string

    required?: boolean | undefined;
>required : boolean

    defaultsTo?: T | undefined;
>defaultsTo : T

};

export type Attribute =
>Attribute : string | StringAttribute | NumberAttribute | OneToOneAttribute

    | string
    | StringAttribute
    | NumberAttribute
    | OneToOneAttribute

export type Attribute2 =
>Attribute2 : string | StringAttribute | NumberAttribute

    | string
    | StringAttribute
    | NumberAttribute

export type StringAttribute = BaseAttribute<string> & {
>StringAttribute : BaseAttribute<string> & { type: 'string'; }

    type: 'string';
>type : "string"

};

export type NumberAttribute = BaseAttribute<number> & {
>NumberAttribute : BaseAttribute<number> & { type: 'number'; autoIncrement?: boolean | undefined; }

    type: 'number';
>type : "number"

    autoIncrement?: boolean | undefined;
>autoIncrement : boolean

};

export type OneToOneAttribute = BaseAttribute<any> & {
>OneToOneAttribute : BaseAttribute<any> & { model: string; }

    model: string;
>model : string

};

// both should error due to excess properties
const attributes: Attribute = {
>attributes : Attribute
>{    type: 'string',    autoIncrement: true,    required: true,} : { type: "string"; autoIncrement: boolean; required: true; }

    type: 'string',
>type : "string"
>'string' : "string"

    autoIncrement: true,
>autoIncrement : boolean
>true : true

    required: true,
>required : true
>true : true

};

const attributes2: Attribute2 = {
>attributes2 : Attribute2
>{    type: 'string',    autoIncrement: true,    required: true,} : { type: "string"; autoIncrement: boolean; required: true; }

    type: 'string',
>type : "string"
>'string' : "string"

    autoIncrement: true,
>autoIncrement : boolean
>true : true

    required: true,
>required : true
>true : true

};