Code Monkey home page Code Monkey logo

Comments (10)

john30 avatar john30 commented on May 22, 2024 1

a rather simple way of allowing use of generics is to patch the regex in toSafeString() in src/utils.ts to also allow "<" and ">" being part of TS identifier and then use e.g.

title: Typename<Generic>

from json-schema-to-typescript.

bcherny avatar bcherny commented on May 22, 2024

This is an interesting idea. I guess there are 2 parts:

  1. Inferring that property types are generic - We can either:
    a. Assume that types that begin with a capital letter are generic
    b. Use an explicit JSON-Schema extension, eg. "genericType": "T"
    c. Don't special case, and let the consumer handle it (as in your example)
  2. Binding concrete types to generic types:
    a. Use an explicit JSON-Schema extension (as in your example)
    b. ???

from json-schema-to-typescript.

goodmind avatar goodmind commented on May 22, 2024

Explicit is always better than implicit, but for first part I was assuming that it will infer types from title <T, R>

from json-schema-to-typescript.

bcherny avatar bcherny commented on May 22, 2024

Inferring from title is too unstructured for my taste, and makes parts 1 and 2 above asymmetrical. Also, someone might for some reason have "IArg<T, R>" as a title, but not intend it as a generic interface. I'd go for explicit all the way through (options 1b and 2a). What do you think?

from json-schema-to-typescript.

goodmind avatar goodmind commented on May 22, 2024

maybe you are right

from json-schema-to-typescript.

goodmind avatar goodmind commented on May 22, 2024

@bcherny Yes, you are right explicit is better here too

from json-schema-to-typescript.

joseSantacruz avatar joseSantacruz commented on May 22, 2024

Any update on this new feature would be great to have and also add conditionals types

from json-schema-to-typescript.

bcherny avatar bcherny commented on May 22, 2024

@joseSantacruz What’s your use case for generic and conditional types in JSON-Schema?

from json-schema-to-typescript.

haggholm avatar haggholm commented on May 22, 2024

My use case (and the reason for my fork) is opaque containers. For example, our application has a lot of encrypted data, so our API frequently sends something of type CryptoContainer with predictable fields for ciphertext, algorithm options, &c., but this doesn’t provide information about what is encrypted. Hence, I added generic parameters like CryptoContainer<T> so that, though this won’t be enforced by JSON schema validation, at least the server and client can agree on the protocol.

from json-schema-to-typescript.

Guchman avatar Guchman commented on May 22, 2024

That's necessary for container-contracts: events, commands, etc. We faced same problem with protobuffs and there is only workaround to use something like Any, but I don't have an idea how to do same with json-schema.
It's really hard to support DTOs like CutstomerEvent, ApplicationEvent, PaymentEvent instead of Event, for example Event.

from json-schema-to-typescript.

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.