TypeScript/tests/cases/compiler/contextuallyTypedByDiscriminableUnion.ts

26 lines
368 B
TypeScript
Raw Normal View History

// @noImplicitAny: true
type ADT = {
kind: "a",
method(x: string): number;
} | {
kind: "b",
method(x: number): string;
};
function invoke(item: ADT) {
if (item.kind === "a") {
item.method("");
}
else {
item.method(42);
}
}
invoke({
kind: "a",
method(a) {
return +a;
}
});