【发布时间】:2021-08-14 01:17:29
【问题描述】:
我有下面的例子。
enum Foo {
bar,
baz
}
interface IBar {
qux: number
}
interface IBaz {
quux: string
}
type InterfaceType<T> =
T extends Foo.bar ? IBar :
T extends Foo.baz ? IBaz : never;
interface ICorge<T> {
foo: T
attributes: InterfaceType<T>
}
const grault: Array<ICorge<unknown>> = [
{
foo: Foo.bar,
attributes: {
qux: 404
}
}, {
foo: Foo.baz,
attributes: {
quux: "not found"
}
}
]
我希望从foo 的类型自动推断出我的接口ICorge 的类型。有没有办法在打字稿中正确地做到这一点?
【问题讨论】:
标签: angular typescript typescript-generics