json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

when schema is: {type: "object", default: "foo"}, the result type is: type Schema = string;

Open inceptiongt opened this issue 3 years ago • 2 comments

when "type" value is mismatch "defualt" value's type, the resutl is wrong.

This: { "type": "object", "default": "foo" }

Results in: export type Schema = string;

While I would have expected: export interface Schema { propertiesA: ... propertiesB: ... }

Without "default" or "default": {} it works fine.

inceptiongt avatar Jan 16 '22 06:01 inceptiongt

What does it mean for default to be a string, while the type of the schema is an object?

JSON-Schemas accept the combination of the different types they may be; in your case, because default is a string, that tells JSTT that the schema could be a string; JSTT then ignores type (which is a bug). The right behavior is to accept either an object or a string. Would you prefer the following type?:

interface Schema1 {
  [k: string]: unknown
}

export type Schema = Schema1 | string

bcherny avatar May 22 '22 04:05 bcherny