Comments (3)
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.
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.
Awesome! Thanks!
from json-schema-to-ts.
Related Issues (20)
- Comparison with `json-schema-to-typescript` HOT 1
- FromSchema returns unknown (code example) HOT 3
- Default attribute should make the property required HOT 5
- VSCode syntax checking slow HOT 3
- 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
- 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 HOT 1
- [Feature] add support for `dependencies` keyword
- Support Draft 2020-12 changes to items and additionalItems
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.