Giter Club home page Giter Club logo

Comments (17)

renewooller avatar renewooller commented on July 18, 2024 1

Well, I've got an ugly(?) implementation working for Partial. Will test and do a pull request.

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024 1

Fixed in #ebad910

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

@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.

robertdempsey avatar robertdempsey commented on July 18, 2024

@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.

domoritz avatar domoritz commented on July 18, 2024

Thanks!

from ts-json-schema-generator.

renewooller avatar renewooller commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

https://astexplorer.net/ is super helpful to see what is different.

from ts-json-schema-generator.

renewooller avatar renewooller commented on July 18, 2024

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.

renewooller avatar renewooller commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

Have you ever considered using simple-ast?

No, I didn't know about it. Looks neat.

from ts-json-schema-generator.

renewooller avatar renewooller commented on July 18, 2024

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.

domoritz avatar domoritz commented on July 18, 2024

Awesome. I'll review it once it's cleaned up.

from ts-json-schema-generator.

renewooller avatar renewooller commented on July 18, 2024

That's all linted.. Looking at Pick now.

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

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)

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.