ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.44k stars 31 forks source link

JSONSchema type does not support array as default #80

Closed StanHannebelle closed 2 years ago

StanHannebelle commented 2 years ago

Defining an array as default value results in a ts-error

import { JSONSchema } from 'json-schema-to-ts';

// Works fine
export const validSchema: JSONSchema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      default: 'stan',
    },
  },
  required: ['name'],
  additionalProperties: false,
} as const;

// Results in a ts-error
export const invalidSchema: JSONSchema = {
  type: 'object',
  properties: {
    names: {
      type: 'array',
      items: { type: 'string' },
      default: ['thomas', 'stan'],
    },
  },
  required: ['names'],
  additionalProperties: false,
} as const;

Here is the resulting ts-error:

const invalidSchema: JSONSchema7 Type '{ readonly type: "object"; readonly properties: { readonly names: { readonly type: "array"; readonly items: { readonly type: "string"; }; readonly default: readonly ["thomas", "stan"]; }; }; readonly required: readonly [...]; readonly additionalProperties: false; }' is not assignable to type 'JSONSchema7'. Types of property 'properties' are incompatible. Type '{ readonly names: { readonly type: "array"; readonly items: { readonly type: "string"; }; readonly default: readonly ["thomas", "stan"]; }; }' is not assignable to type 'Record<string, JSONSchema7> | { readonly [x: string]: boolean | { readonly $id?: string | undefined; readonly $ref?: string | undefined; readonly $schema?: string | undefined; ... 44 more ...; readonly examples?: readonly unknown[] | undefined; }; } | undefined'. Types of property 'names' are incompatible. Type '{ readonly type: "array"; readonly items: { readonly type: "string"; }; readonly default: readonly ["thomas", "stan"]; }' is not assignable to type 'JSONSchema7 | { readonly $id?: string | undefined; readonly $ref?: string | undefined; readonly $schema?: string | undefined; readonly $comment?: string | undefined; ... 43 more ...; readonly examples?: readonly unknown[] | undefined; } | undefined'. Types of property 'default' are incompatible. Type 'readonly ["thomas", "stan"]' is not assignable to type 'JSONSchema7Type | { readonly [x: string]: string | number | boolean | ... | { readonly [x: number]: string | number | boolean | ... | ... | null; readonly length: number; readonly toString: {}; ... 32 more ...; readonly [Symbol.unscopables]: {}; } | null; } | { ...; } | undefined'. Type 'readonly ["thomas", "stan"]' is not assignable to type '{ readonly [x: string]: string | number | boolean | ... | { readonly [x: number]: string | number | boolean | ... | ... | null; readonly length: number; readonly toString: {}; readonly toLocaleString: {}; readonly pop: {}; ... 30 more ...; readonly [Symbol.unscopables]: {}; } | null; }'. Index signature for type 'string' is missing in type 'readonly ["thomas", "stan"]'.ts(2322)

ThomasAribart commented 2 years ago

@StanHannebelle fixed in v2.5.4 👍

StanHannebelle commented 2 years ago

Amazing! Thanks for the great work @ThomasAribart