在Typescript 3中,我使用了来自DefinitelyTyped的定义,但我需要编辑类型别名。我使用@types/json-schema来定义TS,它很容易使用。但是因为我想添加一个自定义的模式类型,所以当我的类型不是已定义的类型时,它显然会报错。例如,我有这样的模式:
{
properties: {
foo: { type: 'myCustomFormat' }
},
required: [
'foo'
],
type: 'object'
}当然,myCustomFormat不是有效的json模式类型。@ types /json-schema通过以下方式定义这些类型:
export type JSONSchema4TypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null' | 'any'有没有办法在我自己的类型定义中修改该类型别名,并更新所有对JSONSchema4TypeName的使用,因为@types/json-schema显然在它的定义中进一步使用了JSONSchema4TypeName?所以就像我想做的那样:
import { JSONSchema4TypeName } from 'json-schema';
export type JSONSchema4TypeName = JSONSchema4TypeName | 'myCustomFormat';我能做的最接近的情况是:
import { JSONSchema4, JSONSchema4TypeName } from 'json-schema';
export type JSONSchema4TypeName = JSONSchema4TypeName | 'uuid';
export interface JSONSchema4 {
type?: JSONSchema4TypeName | JSONSchema4TypeName[];
}我从我的定义中导入了JSONSchema4,而不是json-schema,这感觉像是创建了技术债务,所以我想知道是否有更好的方法。
发布于 2018-09-27 08:01:22
您不能使用扩充覆盖类型别名。您惟一的其他选择是在您的项目中派生@types/json-schema包;有关可能的方法,请参阅this answer。
https://stackoverflow.com/questions/52523547
复制相似问题