Comments (9)
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.
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.
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.
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.
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.
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.
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.
@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.
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)
- Problem with dates HOT 1
- List on json-schema.org HOT 1
- Dynamically create a schema from a function HOT 1
- Simplest generic function doesn't work, because "too complex"? HOT 2
- Combining 'allOf' with 'If' / 'then' checks Do not resolve correctly in TS HOT 2
- Performance Issues with Generics HOT 2
- Definitons in references not resolved
- Properties with const are not typed as const. HOT 2
- Can no longer import JSONSchema7 HOT 1
- Types not resolving correctly after 2.9.2 HOT 3
- Getting `unknown` as result type HOT 1
- JSDoc sees type as never when marking some properties as required HOT 3
- Question: Is there a way to combine or use FromSchema with imported JSON schema
- $ref on definition and additionalProperties: false together results to never type
- [Feature] add support for `dependencies` keyword
- Support Draft 2020-12 changes to items and additionalItems
- TypeScript error when using `nullable` in a definition schema HOT 2
- ...unknown[] for objects of array HOT 1
- allOf operator and ajv-keywords support 2
- Issue: Excessive Nesting and Long Queries Prevent Utilization of Library with Generics HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-schema-to-ts.