我正在绞尽脑汁为什么以下代码无法编译:
interface Circle { type: "circle"; }
interface Rectangle { type: "rectangle"; }
type Shape = Circle | Rectangle;
interface Circle { type: "circle"; }
interface Rectangle { type: "rectangle"; }
type ShapeTemplate = {
type: Shape["type"];
};
const fromTemplate = (template: ShapeTemplate): Shape => template;
Run Code Online (Sandbox Code Playgroud)
它打破了:
TS2322: Type 'ShapeTemplate' is not assignable to type 'Shape'. Type 'ShapeTemplate' is not assignable to type 'Rectangle'. Types of property 'type' are incompatible. Type '"circle" | "rectangle"' is not assignable to type '"rectangle"'. …