Closed
Description
Based on this comment TypeScript does not allow exclusive union types.
I'm proposing a logical or operator similar to union (|
) or intersection (&
) operators that allows defining types that are one or another.
Code
type Person = { name: string; } ^ { firstname: string; lastname: string; };
const p1: Person = { name: "Foo" };
const p2: Person = { firstname: "Foo", lastname: "Bar" } ;
const bad1: Person = { name: "Foo", lastname: "Bar" }
~~~~~~~~~~~~~~~ Type Person can not have name and firstname together
const bad2: Person = { lastname: "Bar", name: "Foo" }
~~~~~~~~~~~ Type Person can not have lastname and name together
For literal and primitive types it should behave like union type:
// These are the same
type stringOrNumber = string | number;
type stringORNumber = string ^ number;