1
- import { expectError , expectType } from './utils' ;
2
- import {
1
+ import type {
2
+ OmitFirstParameters ,
3
+ ConvertOptionalPart ,
4
+ FlattenIntersection ,
3
5
CheckDuplicateKey ,
4
- DuplicateKeys ,
5
- IfEquals ,
6
6
IfEqualsReverse ,
7
- OmitFirstParameters ,
7
+ ConvertOptional ,
8
+ DuplicateKeys ,
9
+ ReverseObject ,
8
10
OptionalKeys ,
9
- PublicOnly ,
10
11
ReadonlyKeys ,
11
12
RequiredKeys ,
12
13
RequiredOnly ,
13
- UrlParams ,
14
14
WritableKeys ,
15
15
DeepReadonly ,
16
16
RequiredPart ,
17
17
PartialPart ,
18
+ PublicOnly ,
18
19
TupleToObj ,
19
- ConvertOptionalPart ,
20
- ConvertOptional ,
21
- FlattenIntersection ,
20
+ UrlParams ,
21
+ IfEquals ,
22
22
Mix ,
23
- ReverseObject ,
24
23
} from '../src' ;
24
+ import { expectError , expectType } from './utils' ;
25
+ import type { Writeable } from '../src' ;
25
26
import * as console from 'console' ;
26
- import { Writeable } from '../src' ;
27
27
28
28
const ab : 'a' | 'b' = Math . random ( ) > 0.5 ? 'a' : 'b' ;
29
29
const abc : 'a' | 'b' | 'c' = Math . random ( ) > 0.5 ? 'a' : Math . random ( ) > 0.5 ? 'b' : 'c' ;
@@ -65,7 +65,7 @@ describe('object', () => {
65
65
expectError < T > ( 9 ) ;
66
66
} ) ;
67
67
test ( 'ReadonlyKeys' , ( ) => {
68
- type T = ReadonlyKeys < { readonly a : number ; b : string ; readonly c : boolean } > ; // 'a'|'c'
68
+ type T = ReadonlyKeys < { readonly c : boolean ; readonly a : number ; b : string } > ; // 'a'|'c'
69
69
70
70
expectType < T > ( 'a' ) ;
71
71
expectType < T > ( 'c' ) ;
@@ -75,7 +75,7 @@ describe('object', () => {
75
75
expectError < T > ( 'd' ) ;
76
76
} ) ;
77
77
test ( 'WritableKeys' , ( ) => {
78
- type T = WritableKeys < { readonly a : number ; b : string ; readonly c : boolean ; d : string } > ; // 'b'|'d'
78
+ type T = WritableKeys < { readonly c : boolean ; readonly a : number ; b : string ; d : string } > ; // 'b'|'d'
79
79
expectType < T > ( 'b' ) ;
80
80
expectType < T > ( 'd' ) ;
81
81
// @ts -expect-error
@@ -84,7 +84,7 @@ describe('object', () => {
84
84
expectError < T > ( 'c' ) ;
85
85
} ) ;
86
86
test ( 'RequiredKeys' , ( ) => {
87
- type T = RequiredKeys < { a : string ; b ?: number ; c : boolean } > ; // 'a'|'c'
87
+ type T = RequiredKeys < { b ?: number ; c : boolean ; a : string } > ; // 'a'|'c'
88
88
expectType < T > ( 'a' ) ;
89
89
expectType < T > ( 'c' ) ;
90
90
// @ts -expect-error
@@ -93,7 +93,7 @@ describe('object', () => {
93
93
expectError < T > ( 'd' ) ;
94
94
} ) ;
95
95
test ( 'RequiredKeys' , ( ) => {
96
- type T = OptionalKeys < { a : string ; b ?: number ; c : boolean } > ; // 'b'
96
+ type T = OptionalKeys < { b ?: number ; c : boolean ; a : string } > ; // 'b'
97
97
expectType < T > ( 'b' ) ;
98
98
// @ts -expect-error
99
99
expectError < T > ( 'a' ) ;
@@ -102,25 +102,25 @@ describe('object', () => {
102
102
} ) ;
103
103
test ( 'RequiredOnly' , ( ) => {
104
104
interface I {
105
- a : string ;
105
+ c : undefined | boolean ;
106
106
b ?: number ;
107
- c : boolean | undefined ;
107
+ a : string ;
108
108
}
109
109
type T = RequiredOnly < I > ; // {a: string; c: boolean | undefined}
110
110
type T2 = OptionalKeys < I > ; // "b"
111
- expectType < T > ( { a : '' , c : undefined } ) ;
112
- expectType < T > ( { a : '' , c : true } ) ;
111
+ expectType < T > ( { c : undefined , a : '' } ) ;
112
+ expectType < T > ( { c : true , a : '' } ) ;
113
113
expectType < T2 > ( 'b' ) ;
114
114
// @ts -expect-error
115
115
expectError < T > ( { a : '' , c : 1 } ) ;
116
116
// @ts -expect-error
117
- expectError < T > ( { a : 1 , c : false } ) ;
117
+ expectError < T > ( { c : false , a : 1 } ) ;
118
118
} ) ;
119
119
test ( 'PublicOnly' , ( ) => {
120
120
class Foo {
121
- public a = '' ;
122
- protected b = 2 ;
123
121
private c = false ;
122
+ protected b = 2 ;
123
+ public a = '' ;
124
124
125
125
constructor ( ) {
126
126
console . log ( this . c ) ;
@@ -224,26 +224,26 @@ describe('object', () => {
224
224
} ) ;
225
225
test ( 'DeepReadonly' , ( ) => {
226
226
interface A {
227
- a : number ;
228
- b : string ;
229
227
c : {
230
228
d : boolean ;
231
229
f : string ;
232
230
} ;
231
+ a : number ;
232
+ b : string ;
233
233
}
234
234
235
235
type B = DeepReadonly < A > ;
236
236
237
237
interface C {
238
- readonly a : number ;
239
- readonly b : string ;
240
238
readonly c : DeepReadonly < {
241
239
d : boolean ;
242
240
f : string ;
243
241
} > ;
242
+ readonly a : number ;
243
+ readonly b : string ;
244
244
}
245
245
246
- const b : B = { a : 1 , b : '' , c : { d : true , f : '' } } ;
246
+ const b : B = { c : { d : true , f : '' } , b : '' , a : 1 } ;
247
247
248
248
// @ts -expect-error
249
249
b . c . d = false ; // error 不能修改.c.d,ts检查会报错
@@ -266,10 +266,10 @@ describe('object', () => {
266
266
267
267
const obj2 : RequiredPart < O , 'a' > = { a : 1 , c : 2 } ;
268
268
269
- expectType < number | undefined > ( obj . a ) ;
269
+ expectType < undefined | number > ( obj . a ) ;
270
270
// @ts -expect-error
271
271
expectError < number > ( obj . a ) ;
272
- expectType < number | undefined > ( obj . b ) ;
272
+ expectType < undefined | number > ( obj . b ) ;
273
273
// @ts -expect-error
274
274
expectError < number > ( obj . b ) ;
275
275
expectType < number > ( obj . c ) ;
@@ -280,15 +280,15 @@ describe('object', () => {
280
280
expectType < number > ( obj2 . c ) ;
281
281
282
282
expectError < {
283
- a : number ;
284
283
b ?: number ;
284
+ a : number ;
285
285
c : number ;
286
286
// @ts -expect-error
287
287
} > ( obj ) ;
288
288
289
289
expectType < {
290
- a : number ;
291
290
b ?: number ;
291
+ a : number ;
292
292
c : number ;
293
293
} > ( obj2 ) ;
294
294
} ) ;
@@ -306,8 +306,8 @@ describe('object', () => {
306
306
307
307
expectType < {
308
308
a ?: number ;
309
- b : number ;
310
309
c ?: number ;
310
+ b : number ;
311
311
} > ( obj ) ;
312
312
} ) ;
313
313
test ( 'TupleToObj' , ( ) => {
@@ -324,10 +324,10 @@ describe('object', () => {
324
324
type T2 = TupleToObj < typeof a , string , 'a' | 'c' , 'dd' > ;
325
325
326
326
expectType < T2 > ( { } ) ;
327
- expectType < Required < T2 > > ( { '1' : '' , '2' : '' , '3' : '' , a : 'dd' , b : '' , c : 'dd ' } ) ;
328
- expectType < T2 > ( { '1' : '' , '2' : '' , '3' : '' , a : 'dd' , b : '' , c : 'dd ' } ) ;
327
+ expectType < Required < T2 > > ( { '1' : '' , '2' : '' , '3' : '' , a : 'dd' , c : 'dd ' , b : '' } ) ;
328
+ expectType < T2 > ( { '1' : '' , '2' : '' , '3' : '' , a : 'dd' , c : 'dd ' , b : '' } ) ;
329
329
// @ts -expect-error
330
- expectError < Required < T2 > > ( { '1' : '' , '2' : '' , '3' : '' , a : 'd' , b : '' , c : 'd ' } ) ;
330
+ expectError < Required < T2 > > ( { '1' : '' , '2' : '' , '3' : '' , a : 'd' , c : 'd ' , b : '' } ) ;
331
331
} ) ;
332
332
333
333
test ( 'ConvertOptionalPart' , ( ) => {
@@ -341,7 +341,7 @@ describe('object', () => {
341
341
342
342
expectType < ConvertOptionalPart < T , 'a' > > ( { } as { a : 1 ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
343
343
expectType < ConvertOptionalPart < T , 'a' > > ( { } as { a : undefined ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
344
- expectType < ConvertOptionalPart < T , 'a' > > ( { } as { a : 1 | undefined ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
344
+ expectType < ConvertOptionalPart < T , 'a' > > ( { } as { a : undefined | 1 ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
345
345
346
346
expectType < ConvertOptionalPart < T , 'a' > > ( { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 } ) ;
347
347
expectType < ConvertOptionalPart < T , 'a' > > ( { a : undefined , b : 2 , c : 3 , d : 4 , e : 5 } ) ;
@@ -402,7 +402,7 @@ describe('object', () => {
402
402
e : 5 ,
403
403
} ) ;
404
404
405
- const v2 = { a : undefined , b : 2 , c : 3 , d : undefined , e : undefined } ;
405
+ const v2 = { a : undefined , d : undefined , e : undefined , b : 2 , c : 3 } ;
406
406
// @ts -expect-error d 和 e 不可设置为 undefined
407
407
expectError < ConvertOptionalPart < T2 , 'a' | 'b' | 'c' > > ( v2 ) ;
408
408
} ) ;
@@ -417,7 +417,7 @@ describe('object', () => {
417
417
418
418
expectType < ConvertOptional < T > > ( { } as { a : 1 ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
419
419
expectType < ConvertOptional < T > > ( { } as { a : undefined ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
420
- expectType < ConvertOptional < T > > ( { } as { a : 1 | undefined ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
420
+ expectType < ConvertOptional < T > > ( { } as { a : undefined | 1 ; b : 2 ; c : 3 ; d : 4 ; e : 5 } ) ;
421
421
422
422
expectType < ConvertOptional < T > > ( { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 } ) ;
423
423
expectType < ConvertOptional < T > > ( { a : undefined , b : 2 , c : 3 , d : 4 , e : 5 } ) ;
@@ -525,19 +525,19 @@ describe('object', () => {
525
525
b : string ;
526
526
}
527
527
528
- const objR : A = { a : 1 , b : 'b' } ;
528
+ const objR : A = { b : 'b' , a : 1 } ;
529
529
// @ts -expect-error
530
530
objR . a = 2 ;
531
531
532
- const objW : Writeable < A > = { a : 1 , b : 'b' } ;
532
+ const objW : Writeable < A > = { b : 'b' , a : 1 } ;
533
533
objW . a = 2 ;
534
534
535
535
type B = Readonly < A > ;
536
- const objR2 : B = { a : 1 , b : 'b' } ;
536
+ const objR2 : B = { b : 'b' , a : 1 } ;
537
537
// @ts -expect-error
538
538
objR2 . b = 'c' ;
539
539
540
- const objW2 : Writeable < B > = { a : 1 , b : 'b' } ;
540
+ const objW2 : Writeable < B > = { b : 'b' , a : 1 } ;
541
541
objW2 . b = 'c' ;
542
542
} ) ;
543
543
@@ -548,6 +548,6 @@ describe('object', () => {
548
548
// @ts -expect-error
549
549
expectError < ReverseObject < { a : 1 ; b : 2 ; c : 3 } > > ( { 1 : 'a' , 2 : 'b' } ) ;
550
550
// @ts -expect-error
551
- expectError < ReverseObject < { a : 1 ; b : 2 ; c : 3 } > > ( { 0 : 1 , 1 : 'a' , 2 : 'b' } ) ;
551
+ expectError < ReverseObject < { a : 1 ; b : 2 ; c : 3 } > > ( { 1 : 'a' , 2 : 'b' , 0 : 1 } ) ;
552
552
} ) ;
553
553
} ) ;
0 commit comments