0

Using Typescript 3, I'm using a definition from DefinitelyTyped but I need to edit a type alias. I am using @types/json-schema for the TS definitions which is easy to use. 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. @types/json-schema defines these types via:

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? 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.

Mitchell Simoens
  • 2,516
  • 2
  • 20
  • 29

1 Answers1

0

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; see this answer for the possible ways of doing that.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75