4d4ea66a9c
This diff extends the types checked by discriminateContextualTypeByObjectMembers and discriminateContextualTypeByJSXAttributes to also include any optional components in the type union. fixes #41759 although it doesn't address the better error reporting for their last repro, which I'm not sure how to address.
98 lines
3.6 KiB
Plaintext
98 lines
3.6 KiB
Plaintext
=== tests/cases/compiler/discriminantPropertyInference.ts ===
|
|
// Repro from #41759
|
|
|
|
type DiscriminatorTrue = {
|
|
>DiscriminatorTrue : Symbol(DiscriminatorTrue, Decl(discriminantPropertyInference.ts, 0, 0))
|
|
|
|
disc: true;
|
|
>disc : Symbol(disc, Decl(discriminantPropertyInference.ts, 2, 26))
|
|
|
|
cb: (x: string) => void;
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 3, 15))
|
|
>x : Symbol(x, Decl(discriminantPropertyInference.ts, 4, 9))
|
|
}
|
|
|
|
type DiscriminatorFalse = {
|
|
>DiscriminatorFalse : Symbol(DiscriminatorFalse, Decl(discriminantPropertyInference.ts, 5, 1))
|
|
|
|
disc?: false;
|
|
>disc : Symbol(disc, Decl(discriminantPropertyInference.ts, 7, 27))
|
|
|
|
cb: (x: number) => void;
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 8, 17))
|
|
>x : Symbol(x, Decl(discriminantPropertyInference.ts, 9, 9))
|
|
}
|
|
|
|
type Props = DiscriminatorTrue | DiscriminatorFalse;
|
|
>Props : Symbol(Props, Decl(discriminantPropertyInference.ts, 10, 1))
|
|
>DiscriminatorTrue : Symbol(DiscriminatorTrue, Decl(discriminantPropertyInference.ts, 0, 0))
|
|
>DiscriminatorFalse : Symbol(DiscriminatorFalse, Decl(discriminantPropertyInference.ts, 5, 1))
|
|
|
|
declare function f(options: DiscriminatorTrue | DiscriminatorFalse): any;
|
|
>f : Symbol(f, Decl(discriminantPropertyInference.ts, 12, 52))
|
|
>options : Symbol(options, Decl(discriminantPropertyInference.ts, 14, 19))
|
|
>DiscriminatorTrue : Symbol(DiscriminatorTrue, Decl(discriminantPropertyInference.ts, 0, 0))
|
|
>DiscriminatorFalse : Symbol(DiscriminatorFalse, Decl(discriminantPropertyInference.ts, 5, 1))
|
|
|
|
// simple inference
|
|
f({
|
|
>f : Symbol(f, Decl(discriminantPropertyInference.ts, 12, 52))
|
|
|
|
disc: true,
|
|
>disc : Symbol(disc, Decl(discriminantPropertyInference.ts, 17, 3))
|
|
|
|
cb: s => parseInt(s)
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 18, 15))
|
|
>s : Symbol(s, Decl(discriminantPropertyInference.ts, 19, 7))
|
|
>parseInt : Symbol(parseInt, Decl(lib.es5.d.ts, --, --))
|
|
>s : Symbol(s, Decl(discriminantPropertyInference.ts, 19, 7))
|
|
|
|
});
|
|
|
|
// simple inference
|
|
f({
|
|
>f : Symbol(f, Decl(discriminantPropertyInference.ts, 12, 52))
|
|
|
|
disc: false,
|
|
>disc : Symbol(disc, Decl(discriminantPropertyInference.ts, 23, 3))
|
|
|
|
cb: n => n.toFixed()
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 24, 16))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 25, 7))
|
|
>n.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 25, 7))
|
|
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
|
|
});
|
|
|
|
// simple inference when strict-null-checks are enabled
|
|
f({
|
|
>f : Symbol(f, Decl(discriminantPropertyInference.ts, 12, 52))
|
|
|
|
disc: undefined,
|
|
>disc : Symbol(disc, Decl(discriminantPropertyInference.ts, 29, 3))
|
|
>undefined : Symbol(undefined)
|
|
|
|
cb: n => n.toFixed()
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 30, 20))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 31, 7))
|
|
>n.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 31, 7))
|
|
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
|
|
});
|
|
|
|
// requires checking type information since discriminator is missing from object
|
|
f({
|
|
>f : Symbol(f, Decl(discriminantPropertyInference.ts, 12, 52))
|
|
|
|
cb: n => n.toFixed()
|
|
>cb : Symbol(cb, Decl(discriminantPropertyInference.ts, 35, 3))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 36, 7))
|
|
>n.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
>n : Symbol(n, Decl(discriminantPropertyInference.ts, 36, 7))
|
|
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
|
|
|
});
|
|
|