我有一个复杂的数据模型,我需要在其中定义40-50单例"元数据"对象:
我希望能够保留为对象文字创建的隐式类型,以便我可以使用intellisense以类型安全的方式操作这些40-50个对象。
以下是使用typescript's documentation for Advanced Types中的形状的示例:
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
type Shape = Square | Rectangle | Circle;
type Shapes = { [x: string]: Shape };
interface Canvas {
name: string;
size: number;
//etc
}
interface Drawing extends Canvas {
shapes: Shapes;
}
//compiles but does not give me intellisense on drawingExplicitType.shapes because it is a Dictionary
let drawingExplicitType: Drawing = {
name: 'myDrawing',
size: 100,
shapes: {
c: { kind: "circle", radius: 1 },
r: { kind: "rectangle", width: 1, height: 1 },
}
};
//compiles and gives me intellisense on drawingImplicitType.shapes
let drawingImplicitType = {
name: 'myDrawing',
size: 100,
shapes: {
c: { kind: "circle", radius: 1 },
r: { kind: "rectangle", width: 1, height: 1 },
}
};
//will have many objects like drawingImplicitType (around 40-50) which I want to manipulate in a type safe manner
//also the shapes will have sub-shapes, so it's a tree-like data model
//I still want to be able to put all "drawing*" objects in an array of Drawing(s)
//ERROR!!! does not compile: Type 'string' is not assignable to type '"circle"'.
let drawing: Drawing = drawingImplicitType;
//## workaround ##########################################
class Drawing2 implements Canvas {
name: string;
size: number;
shapes: Shapes;
fromObjLiteral<T extends Pick<Drawing2, Exclude<keyof Drawing2, 'shapes' | 'fromObjLiteral'>> & {shapes: any}>(
obj: T & {shapes: {readonly [x in keyof T['shapes']]: Shape}}): Drawing2
{
Object.assign(this, obj);
return this;
}
}
//compiles and gives me intellisense on drawingImplicitType2.shapes
let drawingImplicitType2 = new (class {
name = 'myDrawing';
size = 100;
shapes = new (class {
c = new (class { readonly kind = "circle"; radius = 1 })();
r = new (class { readonly kind = "rectangle"; width = 1; height = 1 })();
})();
})();
let drawing2: Drawing2 = new Drawing2().fromObjLiteral(drawingImplicitType2);
let array: Drawing2[] = [drawing2];
//The workaround compiles and still allows me to make arrays of Drawing2(s)
//But I have to create new objects and the syntax is super ugly compared to:
//let drawingImplicitType2 = readonly {...obj literal here...}
编辑2018-05-25
由于Aleksey L.指出类型断言更易于使用,但我们需要在所有子对象上使用它们以获得强类型安全性。对于递归的树状数据模型,这可能有点烦人,其中Shape
可以包含其他Shape
但我认为它比new (class {...})()
//type assertions compile but allow me to make all kinds of errors:
let drawingTypeAssertion = {
name: 'myDrawing',
size: 100,
nonExistentField: 123,
shapes: {
c: { kind: "circle", radiusWrongField: 1 },
r: { kind: "rectangle", width: 1, height: 1 },
}
};
let drawingTA: Drawing = drawingImplicitType as Drawing;
//#####################################################################
// workaround 2: deep type assertions
//#####################################################################
let drawingDeepTypeAssertion = {
name: 'myDrawing',
size: 100,
nonExistentField: 123,
shapes: {
c: { kind: "circle", radius: 1 } as Shape,
r: { kind: "rectangle", width: 1, height: 1 } as Shape,
}
};
let drawingDTA: Drawing = drawingImplicitType as Drawing;
let array2: Drawing[] = [drawingDTA];
//This workaround compiles and still allows me to make arrays of Drawing2(s)
//no need to create extra objects and the syntax is a bit nicer than new (class {...})()
END EDIT 2018-05-25
解决方法2 的一个重大缺点是,只要执行as Shape
,就会在vscode中丢失隐式类型和定义。
据我了解,问题来自于对象文字是可变的,而受歧视的联合不起作用。主要是因为可变性导致判别字段kind
的类型为string
而不是'square'
或'rectangle'
。
关于深度只读和&#34; const&#34;有很多讨论: https://github.com/Microsoft/TypeScript/issues/10725 https://github.com/Microsoft/TypeScript/issues/15300
当前版本的打字稿是否有比上述更好的解决方法?