繁体   English   中英

从@types类型修改TypeScript类型别名

[英]Modify a TypeScript type alias from @types type

使用打字稿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[];
}

然后从定义而不是json-schema中导入该JSONSchema4 ,这感觉像是在增加技术负担,所以想知道是否有更好的方法。

您不能使用扩充来覆盖类型别名。 您唯一的其他选择是在项目中派生@types/json-schema包; 看到这个答案的可能的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM