Giter Site home page Giter Site logo

Comments (3)

ThomasAribart avatar ThomasAribart commented on June 9, 2024

Hi @dariocravero and thanks for the issue, I never tried using FromSchema in JSDocs before.

My guess is that it comes from the as const statement missing. The @satisfies seems to partially narrow the type of your schemas, which is already great (so "object" is kept as "object" and not widened as string), but it doesn't narrow the required keyword value (it is inferred as string[]) which leads to FromSchema being unable to infer required properties.

Can you add some kind of /** @as const **/ ?

from json-schema-to-ts.

dariocravero avatar dariocravero commented on June 9, 2024

Thanks for the hint @ThomasAribart! I think tried that but it didn't work, however, looking up online I came across this post and the last answer suggests using @type {const} which works if the object is wrapped in () 🤷‍♂️ 😬 .

Eg if I add @type {const} before the object's declaration, it doesn't work:

/**
 * @type {const}
 * @satisfies {import('json-schema-to-ts').JSONSchema}
 */
const schemaRequired2 = {
  type: 'object',
  required: ['input'],
  additionalProperties: false,
  properties: {
    input: {
      type: 'object',
      additionalProperties: false,
      required: ['x'],
      properties: {
        x: { type: 'number' },
      },
    },
  },
}

However, if the definition is wrapping the object in parenthesis and move the type definition right before the group as suggested on the last comment of that post, it works!

const schemaRequired2 =
  /** @type {const} @satisfies {import('json-schema-to-ts').JSONSchema}*/ ({
    type: 'object',
    required: ['input'],
    additionalProperties: false,
    properties: {
      input: {
        type: 'object',
        additionalProperties: false,
        required: ['x'],
        properties: {
          x: { type: 'number' },
        },
      },
    },
  })

Then when I do:

/** @type {import('json-schema-to-ts').FromSchema<typeof schemaRequired2>} */
const valueRequired2 = {
  input: {
    x: 2,
  },
}

I get this as expected:

const valueRequired2: {
    input: {
        x: number;
    };
}

I realise that this is what asConst intends to do in a way but its type definition in JSDocs doesn't seem to work as intended. I tried a few different things but I'm not too clear on how to do hack it out tbh. I'm more than happy with the inline comment solution from above though 😆. I submitted that as a PR on the docs here in case it's useful for others #187.

Thanks again! Loving this library, it makes it sooooo much easier to reason about the schema and gets rid of a lot of extra work! 👏

from json-schema-to-ts.

ThomasAribart avatar ThomasAribart commented on June 9, 2024

Awesome! Thanks!

from json-schema-to-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.