简体   繁体   English

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

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

Using Typescript 3, I'm using a definition from DefinitelyTyped but I need to edit a type alias. 使用打字稿3,我使用的是DefinitelyTyped中的定义,但我需要编辑类型别名。 I am using @types/json-schema for the TS definitions which is easy to use. 我正在使用@ types / json-schema作为易于使用的TS定义。 But since I want to add a custom schema type, when I have a type that isn't one of the defined types it will obviously complain. 但是,由于我要添加自定义架构类型,因此当我拥有的类型不是已定义类型之一时,显然会抱怨。 For example, I have this schema: 例如,我有以下架构:

{
    properties: {
        foo: { type: 'myCustomFormat' }
    },
    required: [
        'foo'
    ],
    type: 'object'
}

And of course myCustomFormat is not a valid json schema type. 当然myCustomFormat不是有效的json模式类型。 @types/json-schema defines these types via: @ types / json-schema通过以下方式定义这些类型:

export type JSONSchema4TypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null' | 'any'

Is there a way to modify that type alias in my own type definition and have all uses of JSONSchema4TypeName updated since @types/json-schema obviously uses JSONSchema4TypeName further in it's definition? 有没有办法在我自己的类型定义中修改该类型别名,并更新所有JSONSchema4TypeName用法,因为@ types / json-schema显然在其定义中进一步使用了JSONSchema4TypeName So like I'd like to do: 所以就像我想做的那样:

import { JSONSchema4TypeName } from 'json-schema';

export type JSONSchema4TypeName = JSONSchema4TypeName | 'myCustomFormat';

The closest I'm able to do is this: 我最能做的是:

import { JSONSchema4, JSONSchema4TypeName } from 'json-schema';

export type JSONSchema4TypeName = JSONSchema4TypeName | 'uuid';

export interface JSONSchema4 {
    type?: JSONSchema4TypeName | JSONSchema4TypeName[];
}

and I import that JSONSchema4 from my definition instead of json-schema and that feels like it creates technical debt so wondering if there was a better way. 然后从定义而不是json-schema中导入该JSONSchema4 ,这感觉像是在增加技术负担,所以想知道是否有更好的方法。

You can't overwrite a type alias using an augmentation. 您不能使用扩充来覆盖类型别名。 Your only other option would be to fork the @types/json-schema package in your project; 您唯一的其他选择是在项目中派生@types/json-schema包; see this answer for the possible ways of doing that. 看到这个答案的可能的方法。

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

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