diff --git a/tests/cases/conformance/types/conditional/inferTypes1.ts b/tests/cases/conformance/types/conditional/inferTypes1.ts index 25552a68acf..6ff7ae67c89 100644 --- a/tests/cases/conformance/types/conditional/inferTypes1.ts +++ b/tests/cases/conformance/types/conditional/inferTypes1.ts @@ -73,3 +73,35 @@ type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number type T60 = infer U; // Error type T61 = infer A extends infer B ? infer C : infer D; // Error type T62 = U extends (infer U)[] ? U : U; // Error + +// Example from #21496 + +type JsonifiedObject = { [K in keyof T]: Jsonified }; + +type Jsonified = + T extends string | number | boolean | null ? T + : T extends undefined | Function ? never // undefined and functions are removed + : T extends { toJSON(): infer R } ? R // toJSON is called if it exists (e.g. Date) + : T extends object ? JsonifiedObject + : "what is this"; + +type Example = { + str: "literalstring", + fn: () => void, + date: Date, + customClass: MyClass, + obj: { + prop: "property", + clz: MyClass, + nested: { attr: Date } + }, +} + +declare class MyClass { + toJSON(): "correct"; +} + +type JsonifiedExample = Jsonified; +declare let ex: JsonifiedExample; +const z1: "correct" = ex.customClass; +const z2: string = ex.obj.nested.attr;