2017-10-17 18:56:28 +02:00
|
|
|
// @strict: true
|
2017-06-08 20:03:52 +02:00
|
|
|
type ADT = {
|
|
|
|
tag: "A",
|
|
|
|
a1: string
|
|
|
|
} | {
|
|
|
|
tag: "D",
|
|
|
|
d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
|
|
|
|
} | {
|
|
|
|
tag: "T",
|
|
|
|
}
|
|
|
|
let wrong: ADT = { tag: "T", a1: "extra" }
|
|
|
|
wrong = { tag: "A", d20: 12 }
|
|
|
|
wrong = { tag: "D" }
|
2017-06-09 18:51:07 +02:00
|
|
|
|
|
|
|
type Ambiguous = {
|
|
|
|
tag: "A",
|
|
|
|
x: string
|
|
|
|
} | {
|
|
|
|
tag: "A",
|
|
|
|
y: number
|
|
|
|
} | {
|
|
|
|
tag: "B",
|
|
|
|
z: boolean
|
|
|
|
} | {
|
|
|
|
tag: "C"
|
|
|
|
}
|
|
|
|
let amb: Ambiguous
|
|
|
|
// no error for ambiguous tag, even when it could satisfy both constituents at once
|
|
|
|
amb = { tag: "A", x: "hi" }
|
|
|
|
amb = { tag: "A", y: 12 }
|
|
|
|
amb = { tag: "A", x: "hi", y: 12 }
|
|
|
|
|
|
|
|
// correctly error on excess property 'extra', even when ambiguous
|
|
|
|
amb = { tag: "A", x: "hi", extra: 12 }
|
|
|
|
amb = { tag: "A", y: 12, extra: 12 }
|
|
|
|
|
|
|
|
// assignability errors still work.
|
|
|
|
// But note that the error for `z: true` is the fallback one of reporting on
|
|
|
|
// the last constituent since assignability error reporting can't find a single best discriminant either.
|
|
|
|
amb = { tag: "A" }
|
|
|
|
amb = { tag: "A", z: true }
|
2017-06-10 00:47:57 +02:00
|
|
|
|
|
|
|
type Overlapping =
|
|
|
|
| { a: 1, b: 1, first: string }
|
|
|
|
| { a: 2, second: string }
|
|
|
|
| { b: 3, third: string }
|
|
|
|
let over: Overlapping
|
|
|
|
|
2017-11-07 01:09:35 +01:00
|
|
|
// these two are still errors despite their doubled up discriminants
|
2017-06-10 00:47:57 +02:00
|
|
|
over = { a: 1, b: 1, first: "ok", second: "error" }
|
|
|
|
over = { a: 1, b: 1, first: "ok", third: "error" }
|
2017-10-17 18:56:28 +02:00
|
|
|
|
|
|
|
// Freshness disappears after spreading a union
|
|
|
|
declare let t0: { a: any, b: any } | { d: any, e: any }
|
|
|
|
declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
|
|
|
|
let t2 = { ...t1 }
|
|
|
|
t0 = t2
|
2018-01-19 00:11:29 +01:00
|
|
|
|
|
|
|
// Nested excess property checks work with discriminated unions
|
|
|
|
type AN = { a: string } | { c: string }
|
|
|
|
type BN = { b: string }
|
|
|
|
type AB = { kind: "A", n: AN } | { kind: "B", n: BN }
|
|
|
|
const abab: AB = {
|
|
|
|
kind: "A",
|
|
|
|
n: {
|
|
|
|
a: "a",
|
|
|
|
b: "b", // excess -- kind: "A"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const abac: AB = {
|
|
|
|
kind: "A",
|
|
|
|
n: {
|
|
|
|
a: "a",
|
|
|
|
c: "c", // ok -- kind: "A", an: { a: string } | { c: string }
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 00:34:52 +02:00
|
|
|
|
|
|
|
// Excess property checks must match all discriminable properties
|
|
|
|
type Button = { tag: 'button'; type?: 'submit'; };
|
|
|
|
type Anchor = { tag: 'a'; type?: string; href: string };
|
|
|
|
|
|
|
|
type Union = Button | Anchor;
|
|
|
|
const obj: Union = {
|
|
|
|
tag: 'button',
|
|
|
|
type: 'submit',
|
|
|
|
|
|
|
|
// should have error here
|
|
|
|
href: 'foo',
|
|
|
|
};
|
2019-11-15 19:59:58 +01:00
|
|
|
|
|
|
|
// Repro from #34611
|
|
|
|
|
|
|
|
interface IValue {
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StringKeys {
|
|
|
|
[propertyName: string]: IValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface NumberKeys {
|
|
|
|
[propertyName: number]: IValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectDataSpecification = StringKeys | NumberKeys;
|
|
|
|
|
|
|
|
|
|
|
|
const dataSpecification: ObjectDataSpecification = { // Error
|
|
|
|
foo: "asdfsadffsd"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Repro from #34611
|
|
|
|
|
|
|
|
const obj1: { [x: string]: number } | { [x: number]: number } = { a: 'abc' }; // Error
|
|
|
|
const obj2: { [x: string]: number } | { a: number } = { a: 5, c: 'abc' }; // Error
|
2020-03-17 22:58:52 +01:00
|
|
|
|
|
|
|
// Repro from #33732
|
|
|
|
|
|
|
|
interface I1 {
|
|
|
|
prop1: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I2 {
|
|
|
|
prop2: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I3 extends Record<string, string> {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type Properties =
|
|
|
|
| { [key: string]: never }
|
|
|
|
| I1
|
|
|
|
| I2
|
|
|
|
| I3
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
declare const prop1: string;
|
|
|
|
declare const prop2: string | undefined;
|
|
|
|
|
|
|
|
function F1(_arg: { props: Properties }) { }
|
|
|
|
F1({
|
|
|
|
props: {
|
|
|
|
prop1,
|
|
|
|
prop2,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function F2(_props: Properties) { }
|
|
|
|
F2({
|
|
|
|
prop1,
|
|
|
|
prop2,
|
|
|
|
});
|