Code Monkey home page Code Monkey logo

Comments (9)

daffl avatar daffl commented on May 17, 2024 3

I've been seein the same error in the latest version in https://github.com/feathersjs/feathers/tree/dove/packages/schema. The smallest example I could come up with to reproduce looks like this:

class Test<S extends JSONSchema> {
  readonly type: FromSchema<S>;
}

const schemaA = {
  type: 'object',
  additionalProperties: false,
  properties: {
    name: { type: 'string' }
  }
} as const;

const schemaB = {
  type: 'object',
  additionalProperties: false,
  properties: {
    name: { type: 'string' }
  }
} as const;

const t: Test<typeof schemaB> = new Test<typeof schemaA>();

Maybe that helps, I can try and do some more digging if you think it's worth investigating.
Either way, this is a great project!

from json-schema-to-ts.

ThomasAribart avatar ThomasAribart commented on May 17, 2024

Hi @ffxsam and thanks for the issue.

Everything seems fine on json-schema-to-ts side. It's the combination of Sentry and FromSchema that seems to generate too many type computations 🤔 I don't even get why ParseMixedSchema<?, T> is mentioned here, as your schema is not mixed (i.e. you do not provide an array of types but simply "object").

Can you try defining ValidatedAPIGatewayProxyEvent<S> in a separate type ?

type Event = ValidatedAPIGatewayProxyEvent<S>

export const handler = Sentry.AWSLambda.wrapHandler<
      Event,
      APIGatewayProxyResult | undefined
    >(...

...or simply, not providing explicitely the generic types to wrapHandler ? (they can be inferred from the handler itself, I think)

const yourFunc = async (event: ValidatedAPIGatewayProxyEvent<S>) : Promise<APIGatewayProxyResult | undefined> => ...

export const handler = Sentry.AWSLambda.wrapHandler(yourFunc)

What version of json-schema-to-ts did you use before having the error ? Was json-schema-to-ts the only library that you upgraded or did you upgrade Typescript as well ?

from json-schema-to-ts.

ffxsam avatar ffxsam commented on May 17, 2024

Hey @ThomasAribart, thanks for the reply!

You'll have to excuse me, as my TypeScript ability is passable at best.

type Event = ValidatedAPIGatewayProxyEvent<S>;

This results in an error, Cannot find name 'S'.

Unfortunately, I can't rely on implicit types here. The code for wrapApiHandler is more complex than illustrated here (it took me hours to figure out). I can post it in its entirety if that helps you.

TypeScript was updated, but I reverted it step by step back to version 4.5.2 and the issue still persisted. Then I reverted from json-schema-to-ts version 1.6.5 to 1.6.4 and that resolved it.

from json-schema-to-ts.

ThomasAribart avatar ThomasAribart commented on May 17, 2024

S is supposed to be the type of your schema, defined with the as const statement:

import { schema } from './schema'

type S = typeof schema

from json-schema-to-ts.

ffxsam avatar ffxsam commented on May 17, 2024

Thanks, Thomas! I appreciate the help, but unfortunately, I don't have the bandwidth to try to track down what's going on. I'm guessing it's due to some sloppy TypeScript that json-schema-to-ts was previously more forgiving about. I'll pin my project to version 1.6.4 and call it a wrap.

from json-schema-to-ts.

ericvicenti avatar ericvicenti commented on May 17, 2024

Oop, I made a duplicate issue here: #56

Filed here because I haven't seen this error on any other codebases aside from when I use json-schema-to-ts

But now I'm thinking we have exposed some bug in TypeScript.

from json-schema-to-ts.

psznm avatar psznm commented on May 17, 2024

Typescript 4.7, which introduced variance annotations, has been released. I believe variance annotations could help solve this issue. And overall speed up type inference. @ThomasAribart, could you please look into that?

from json-schema-to-ts.

ThomasAribart avatar ThomasAribart commented on May 17, 2024

@daffl @psznm @ffxsam @ericvicenti Indeed, it seems like the variance annotation helps a bit. The smallest example you provided is fixed by declaring S as invariant (i.e. with both in and out annotation).

I guess it skips the FromSchema type computation altogether and call it a day if schemaA and schemaB are equal (or not):

class Test<in out S extends JSONSchema> {
  readonly type?: FromSchema<S>;
}

const schemaA = {
  type: "object",
  additionalProperties: false,
  properties: {
    name: { type: "string" },
  },
} as const;

const schemaB = {
  type: "object",
  additionalProperties: false,
  properties: {
    name: { type: "string" },
  },
} as const;

// works fine
const test: Test<typeof schemaB> = new Test<typeof schemaA>();

However, I'm not completely sure this fixes the issue ? 🤔

I've found that defining the expected type as the default value of a second generic type is a functioning work-around though:

// "out" annotation is welcome but not necessary
class Test<S extends JSONSchema, out T = FromSchema<S>> {
  readonly type?: T;
}

const schemaA = {
  type: "object",
  additionalProperties: false,
  properties: {
    name: { type: "string" },
  },
  // this time, you can have different specs between schemas
  required: ['name']
} as const;

const schemaB = {
  type: "object",
  additionalProperties: false,
  properties: {
    name: { type: "string" },
  },
} as const;

// works fine
const test: Test<typeof schemaB> = new Test<typeof schemaA>();

Can you confirm that it work ? Does it close this issue ?

from json-schema-to-ts.

ericvicenti avatar ericvicenti commented on May 17, 2024

I think this can be closed. I haven't seen this weirdo error in a while

Now I just need to find workarounds for "Type instantiation is excessively deep and possibly infinite" 😤

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.