Skip to content

Commit cac6b5b

Browse files
committed
Add regression test
1 parent bb28444 commit cac6b5b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/cases/conformance/types/conditional/inferTypes1.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,35 @@ type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number
7373
type T60 = infer U; // Error
7474
type T61<T> = infer A extends infer B ? infer C : infer D; // Error
7575
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;

0 commit comments

Comments
 (0)