Comments (17)
Well, I've got an ugly(?) implementation working for Partial. Will test and do a pull request.
from ts-json-schema-generator.
Fixed in #ebad910
from ts-json-schema-generator.
That's awesome. I built this library to support schema generation in https://github.com/vega/vega-lite and it works really well. I don't have the time right now to look into the particular issue but I'm sure if you step through the execution and put breakpoints in the right spots, you can figure out how to fix this. It looks more complicated than it is.
from ts-json-schema-generator.
Simple example
export type MyObject = Part<B>;
type Part<T> = {
[P in keyof T]?: T[P];
};
interface B {
b: number;
c: string;
}
The reason why it doesn't work is that we need to associate keys with values in Part
. I didn't find an obvious way to do that and don't have time right now but will look at it in a few weeks.
from ts-json-schema-generator.
@robertdempsey it shouldn't take too long to fix this and I would love a PR for this. We could use this to fix an issue here: vega/vega-lite#3491 (comment)
from ts-json-schema-generator.
@domoritz sorry I haven't replied yet. We are a bit stretched for time too but will see if we can take a look. It might take a couple weeks if we do though and it'd be our first time working on this library. Will keep you updated on whether or not we get to take a look. Cheers!
from ts-json-schema-generator.
Thanks!
from ts-json-schema-generator.
Hi, I work with @robertdempsey. I've looked into this a little - it seems that because of the intentional limitation with the TS compiler to not know what is implementing a generic, we're going to have a hard time getting the information we need and fitting it into the way this project is set up.
eg see this issue
I debugged and stepped into MappedTypeNodeParser
. getTypeFromTypeNode
returns a type that doesn't have types
in it, hence the 'undefined' error reported by @robertdempsey.
It ends up at getTypeFromTypeOperatorNode
, where it does getTypeFromTypeNode
on the T
type, which is unknown. So the type we end up with is unknown and can't have any properties extracted from it.
We're keen to help, but I'm not really familiar with this code base, so any help in pointing me to the right direction to solve this kind of problem would be appreciated.
In the meantime I figure you must have solved something like this before in generics, so I'm studying that.
I've noticed that from this.getRootChildDefinitions(rootType)
, generics don't have baseTypes - it could be something to do with that.
from ts-json-schema-generator.
I looked a bit into it yesterday and I can’t say for sure whether this will work but my idea was to figure out all the possible values for P and then getting the type of each property of T. This way we should get Pick and Partial working. Sortify from your example at the top should be easy because the type on the right side does not depend on a key.
from ts-json-schema-generator.
https://astexplorer.net/ is super helpful to see what is different.
from ts-json-schema-generator.
I did had a look at that - still have a bit to learn to make it useful I think. Have you ever considered using simple-ast?
from ts-json-schema-generator.
Just putting notes here to help with dev - no need to respond unless you want to.
So the point at which we're losing the information is in the ExpressionWithTypeArgumentsNodeParser
, when we call this.childNodeParser.createType
, the node is passes through is exactly
type Partial<T> = {
[P in keyof T]?: T[P];
};
and this has none of the type information that would allow the baseType
to have properties.
UPDATE - it's actually available through the context. continuing...
from ts-json-schema-generator.
Have you ever considered using simple-ast?
No, I didn't know about it. Looks neat.
from ts-json-schema-generator.
In the MappedTypeGenerator
the ObjectProperties
can be extracted from the context.arguments
like so:
context.getArguments().reduce((acc : ObjectProperty[], arg : any) => {
acc = acc.concat(arg.getProperties())
return acc;
}, [])
however, they are still 'required'. I'm currently trying to figure out a nice way of ensuring the attributes of the parameters defined by the mapped type are used.
from ts-json-schema-generator.
Awesome. I'll review it once it's cleaned up.
from ts-json-schema-generator.
That's all linted.. Looking at Pick now.
from ts-json-schema-generator.
Fantastic. I'm really excited to have your contribution. There are a couple of pending refactorings that we couldn't do because of missing support for Partial. For instance vega/vega-lite#3502.
from ts-json-schema-generator.
Related Issues (20)
- How to exclude underscore properties from json schema ? HOT 1
- Issues with generation of Union Type of Union Type on a Record HOT 1
- Argument of type 'Config' is not assignable to parameter of type 'CompletedConfig' in ts-json-schema-generator 2.0.0 HOT 2
- "foo" | "bar" | string; should not only map to a string HOT 9
- Improved Promise handling to support packages like Prisma HOT 1
- Literal and Record<never, never> crates empty object HOT 1
- Cannot migrate to version 2 due to typescript error type CompletedConfig HOT 2
- [Bug]: Error when using the binary (v2.1.0) HOT 9
- V2 creates empty definitions after updating from V1 HOT 5
- CLI usage is throwing an error with node 22 HOT 3
- RegExp types are now emitted, breaking schema validation (v2.1.1) HOT 1
- Error when following exports?
- Dist types now require @types/ts-expose-internals HOT 9
- Fix CJS imports HOT 8
- Invalid index in type HOT 10
- CLI - ENOENT: no such file or directory, open 'package.json' HOT 3
- Error: Unknown type "undefined" HOT 2
- Required types not allowed HOT 1
- Generator crashed when no package version is set HOT 4
- Any way to indicate that a string is actually a date-time? HOT 7
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 ts-json-schema-generator.