Comments (10)
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.
This is an interesting idea. I guess there are 2 parts:
- 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) - Binding concrete types to generic types:
a. Use an explicit JSON-Schema extension (as in your example)
b. ???
from json-schema-to-typescript.
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.
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.
maybe you are right
from json-schema-to-typescript.
@bcherny Yes, you are right explicit is better here too
from json-schema-to-typescript.
Any update on this new feature would be great to have and also add conditionals types
from json-schema-to-typescript.
@joseSantacruz What’s your use case for generic and conditional types in JSON-Schema?
from json-schema-to-typescript.
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.
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)
- List on json-schema.org HOT 1
- Create Typscript schema with external references
- Convert empty object to Record<string,never> HOT 3
- Remove optional flag in case of `default` in schema. HOT 8
- `strictIndexSignatures` option is ignored for single pattern properties
- Package lists dev dependencies as dependencies HOT 4
- If `minProperties` is greater than or equal to the number of declared properties and `additionalProperties` is `false`, all properties should be required HOT 2
- properties + anyOf generates incorrect output
- `compile()` ignores `$refOptions.resolve` settings HOT 1
- What package manager does the repo use? HOT 6
- `SyntaxError` while building browser bundle HOT 2
- Add option to remove `[k: string]: unknown | undefined;` from generated types HOT 5
- Add yaml support HOT 3
- Static imports for $refs HOT 4
- bug in vercel production, but development environment is OK HOT 1
- "SyntaxError: Unexpected token in JSON at position 0" when running CLI on MacOS HOT 2
- wrongly transformed array of string HOT 6
- Update to glob 10 HOT 1
- Remove deprecated deps @types/prettier HOT 2
- Indirect dependency on es5-ext: flagged as malicious by some security scanners 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-typescript.