【发布时间】:2021-10-14 14:55:57
【问题描述】:
我正在为 JSON Schema 使用 TS 类型:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/json-schema/index.d.ts
短版:
interface JSONSchema4 {
properties?: {
[k: string]: JSONSchema4;
} | undefined;
}
这种类型是递归的。我想用自定义属性扩展这种类型。例如。目前,类似这样的内容与类型不匹配:
{ instanceof: 'Date' }
我想将instanceof?: string 作为字段添加到JSONSchema4。这可能吗?
【问题讨论】:
-
instanceof是一个保留字,不过你也许可以把它放在引号里……'instanceof': ... -
但是如何扩展递归类型呢?
-
我认为您必须扩展接口并替换接口中可以为
JSONSchema4类型的所有属性以引用您自己的接口类型。它不漂亮,但这是我唯一能想到的东西
标签: javascript typescript jsonschema