ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas šŸ“
MIT License
1.4k stars 30 forks source link

Issue: Excessive Nesting and Long Queries Prevent Utilization of Library with Generics #196

Closed gardevgranade closed 2 months ago

gardevgranade commented 3 months ago

Hi, first of all: Thanks for this amazing tool!!

Problem Description: When utilizing the repository and its functions, there is a recurring issue of encountering overly long and deeply nested queries, rendering the library practically unusable. This problem seems to arise particularly when using generics.

Small simple Example:

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

const testSchema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' },
  },
  required: ['name'],
} as const;

function test<T extends JSONSchema>(schema: T): FromSchema<T> {
  return {
    name: 'Hello',
    age: 12,
  } as FromSchema<T>;
}

Issue Inquiry: I'm unsure if the observed behavior is intended, or if I'm misunderstanding the library's usage. Specifically, I'm uncertain whether utilizing generics in this context is supported and if there's a mistake in my implementation.

Additional Context: While I'm very happy with this library and it essentially provides exactly what I've been searching for, the extensive nesting and complexity hinder the library's usability, particularly when dealing with generics. Understanding the intended behavior would greatly assist in resolving these challenges and effectively leveraging the library's functionality.

Thank you for your attention to this matter. Any guidance or clarification would be greatly appreciated.

gardevgranade commented 2 months ago

Hey, can i do anything here to support you?

gardevgranade commented 2 months ago

Sorry for not reading the faq šŸ™ˆ.

Solution from faq:


type Mocker = <SCHEMA extends JSONSchema, DATA = FromSchema<SCHEMA>>(
  schema: SCHEMA,
) => DATA;