【发布时间】:2016-10-31 05:39:57
【问题描述】:
我想在打字稿中描述以下对象形状,但我想忽略字段的类型。
interface TestInterface {
TestOne?: string;
TestTwo?: number;
TestThree?: boolean;
}
我的想法是用以下方式来描述它:
type Shape = { [fieldName: string]: any };
type ShapeType<TShape extends Shape> = TShape;
var test: ShapeType<TestInterface> = {
TestThree: "asdf",
}
它应该抱怨这样的事情:
var test: ShapeType<TestInterface> = {
TestThree: "asdf",
TestFour: "123",
}
如果我将“asdf”类型转换为任何它可以工作。有没有办法以不需要强制转换的方式来描述这一点?
编辑:其背后的想法是具有通常用于数据交换但在特殊场合将用于元数据的形状。在这些情况下,我只关心结构,而不关心类型(至少现在 - 想法是将形状的类型更改为另一种给定类型)。
【问题讨论】:
标签: typescript