Let\'s say we have the following Typescript interface
:
interface Sample {
key1: boolean;
key2?: string;
key3?: number;
};
Thought I'd mention another nice approach here could be to use a discriminated union:
enum ShapeKind {
Circle,
Square,
}
interface Circle {
kind: ShapeKind.Circle;
radius: number;
}
interface Square {
kind: ShapeKind.Square;
sideLength: number;
}
let c: Circle = {
kind: ShapeKind.Square,
// ~~~~~~~~~~~~~~~~ Error!
radius: 100,
}
As described in the Typescript docs: https://www.typescriptlang.org/docs/handbook/enums.html#union-enums-and-enum-member-types