File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
tests/cases/conformance/types/conditional Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -73,3 +73,35 @@ type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number
73
73
type T60 = infer U ; // Error
74
74
type T61 < T > = infer A extends infer B ? infer C : infer D ; // Error
75
75
type T62 < T > = U extends ( infer U ) [ ] ? U : U ; // Error
76
+
77
+ // Example from #21496
78
+
79
+ type JsonifiedObject < T extends object > = { [ K in keyof T ] : Jsonified < T [ K ] > } ;
80
+
81
+ type Jsonified < T > =
82
+ T extends string | number | boolean | null ? T
83
+ : T extends undefined | Function ? never // undefined and functions are removed
84
+ : T extends { toJSON ( ) : infer R } ? R // toJSON is called if it exists (e.g. Date)
85
+ : T extends object ? JsonifiedObject < T >
86
+ : "what is this" ;
87
+
88
+ type Example = {
89
+ str : "literalstring" ,
90
+ fn : ( ) => void ,
91
+ date : Date ,
92
+ customClass : MyClass ,
93
+ obj : {
94
+ prop : "property" ,
95
+ clz : MyClass ,
96
+ nested : { attr : Date }
97
+ } ,
98
+ }
99
+
100
+ declare class MyClass {
101
+ toJSON ( ) : "correct" ;
102
+ }
103
+
104
+ type JsonifiedExample = Jsonified < Example > ;
105
+ declare let ex : JsonifiedExample ;
106
+ const z1 : "correct" = ex . customClass ;
107
+ const z2 : string = ex . obj . nested . attr ;
You can’t perform that action at this time.
0 commit comments