TypeScript/tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts
Nathan Shively-Sanders b24050aefd
Excess discriminated types match all discriminable properties (#32755)
* Target types in excess property checking must match all discriminable properties

This allows fewer types to be discriminated in excess properties, which
fixes some examples.

* Add excess property test

* Fix semicolon lint

* Remove extra semicolon!

* Improve EPC for unions with multiple discriminants
2019-08-08 15:34:52 -07:00

60 lines
1.2 KiB
TypeScript

// Repro from #32657
interface Base<T> {
value: T;
}
interface Int extends Base<number> {
type: "integer";
multipleOf?: number;
}
interface Float extends Base<number> {
type: "number";
}
interface Str extends Base<string> {
type: "string";
format?: string;
}
interface Bool extends Base<boolean> {
type: "boolean";
}
type Primitive = Int | Float | Str | Bool;
const foo: Primitive = {
type: "number",
value: 10,
multipleOf: 5, // excess property
format: "what?"
}
type DisjointDiscriminants = { p1: 'left'; p2: true; p3: number } | { p1: 'right'; p2: false; p4: string } | { p1: 'left'; p2: boolean };
// This has excess error because variant three is the only applicable case.
const a: DisjointDiscriminants = {
p1: 'left',
p2: false,
p3: 42,
p4: "hello"
};
// This has no excess error because variant one and three are both applicable.
const b: DisjointDiscriminants = {
p1: 'left',
p2: true,
p3: 42,
p4: "hello"
};
// This has excess error because variant two is the only applicable case
const c: DisjointDiscriminants = {
p1: 'right',
p2: false,
p3: 42,
p4: "hello"
};