我不理解为何转播者抱怨。 基本类型申报:
export enum SvgElementType {
path = "path",
circle = "circle",
}
export type SvgElement = {
id: number;
type: SvgElementType;
};
export type SvgPathElement = SvgElement & {
type: SvgElementType.path;
d: string;
};
export type SvgCircleElement = SvgElement & {
type: SvgElementType.circle;
cx: number;
cy: number;
radius: number;
};
export type Serializer<E extends SvgElement = SvgElement> = (element: E) => string;
const pathSerializer: Serializer<SvgPathElement> = e => "";
const circleSerializer: Serializer<SvgCircleElement> = e => "";
const serializers: Set<Serializer> = new Set();
serializers.add(pathSerializer); // <<<--- transpile error
// full TS error
// Argument of type Serializer<SvgPathElement> is not assignable to parameter of type Serializer<SvgElement> .
// Type SvgElement is not assignable to type SvgPathElement .
// Property d is missing in type SvgElement but required in type { type: SvgElementType.path; d: string; } .ts(2345)
我发现的唯一方式是修改<代码>Serializer with any
作为缺省类型:
export type Serializer<E extends SvgElement = any> = (element: E) => string;
这为我指明了,保护<代码>航天器/代码>的最低打字方法可能更好。 3. 稍后采用......