How do I enforce strict subclass typing for object values in the SHAPE_PARAMS
definition? When using type assertion like <CircleParameter>
, missing properties are not caught by linting.
Is there a way to define subclass types strictly?
const Shapes = {
CIRCLE_A: "CIRCLE_A",
CIRCLE_B: "CIRCLE_B",
RECTANGLE_A: "RECTANGLE_A",
} as const;
type Shapes = typeof Shapes[keyof typeof Shapes];
interface ShapeParameter {
color: string;
}
interface CircleParameter extends ShapeParameter {
radius: number;
}
interface RectangleParameter extends ShapeParameter {
width: number;
height: number;
}
const SHAPE_PARAMS: { [key in Shapes]: ShapeParameter } = {
[Shapes.CIRCLE_A]: <CircleParameter>{
color: "red",
radius: 1,
},
[Shapes.CIRCLE_B]: <CircleParameter>{
// color: "blue", <- Missed property cannot be linted.
radius: 2,
},
[Shapes.RECTANGLE_A]: <RectangleParameter>{
color: "green",
width: 3,
// height: 3, <- Missed property cannot be linted.
},
};