【发布时间】:2020-11-29 06:45:54
【问题描述】:
我想创建类型树结构,传递根节点和子节点模式。根据模式,我希望必需的属性是必需的,而不是可选的,因为它必须如此。但是我的类型测试没有通过。这是我的一段代码:
type TypedTree<TRootSchema, TChildSchema = TRootSchema> =
{
[K in keyof TRootSchema]: TRootSchema[K]
} | {
[key: string]: TypedTree<TChildSchema>
}
const $typeTest = <T>(v: T) => v
// @ts-expect-error Required properies must be present
$typeTest<TypedTree<{ a: number }>>({ })
// ???? It fails
【问题讨论】:
-
{ [K in keyof TRootSchema]: TRootSchema[K] }不是和TRootSchema一样吗? -
是的,无论如何,问题仍然存在
标签: javascript typescript testing