0
我想要嵌套对象的类型,它们的叶子上具有特定类型。具有特定叶子类型的嵌套对象的类型
我希望这是正确的:
let a: Leaves<number> = {
"foo": 1,
"bar": {
"baz": 2
}
};
,这失败:
let a: Leaves<number> = {
"foo": 1,
"bar": {
"baz": 'something else' // Leaf of type string
}
};
我试图
export type Leaves<T> = {
[P in keyof T]: T | Leaves<T>;
}
但是与定义这两个例子给出一个编译错误。第一个说Type '{ "foo": number; "bar": number; }' is not assignable to type 'number'.
有没有办法解决这个问题?
谢谢,这很有帮助。猜猜我有点困惑。 – MaximeW