Support Pick and other Mapped Types
See original GitHub issueHi! Great library here. We are heavily experimenting with using json schemas generated from our TypeScript interfaces on both front end and back end and this is obviously the glue that’s holding it all together! With that said, there are some scenarios we are encountering that we are having to work around to get our schemas generated the way we want to, the main issues being around Mapped Types.
Consider the following interfaces:
export interface ISortableProperties {
/**
* @description id
*/
id: number;
/**
* @description name
*/
name: string;
/**
* @description date
*/
date: Date;
}
export interface IResponse {
unsortableProperty: string;
}
export interface IRequest {
sortBy: Sortify<Pick<ISortableProperties, "id">> | Sortify<Pick<ISortableProperties, "name">> | Sortify<Pick<ISortableProperties, "date">>
}
export type Sortify<T> = {
[P in keyof T]: sorting.SortDirection
}
The schema I’d like to see from this is
{
"$schema": "http://json-schema.org/draft-06/schema#",
"additionalProperties": false,
"definitions": {
"sorting.SortDirection": {
"enum": [
"ASC",
"DESC"
],
"type": "string"
}
},
"properties": {
"sortBy": {
"anyOf": [
{
"additionalProperties": false,
"properties": {
"id": {
"$ref": "#/definitions/sorting.SortDirection",
"description": "id"
}
},
"required": [
"id"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"name": {
"$ref": "#/definitions/sorting.SortDirection",
"description": "name"
}
},
"required": [
"name"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"date": {
"$ref": "#/definitions/sorting.SortDirection",
"description": "date"
}
},
"required": [
"date"
],
"type": "object"
}
]
}
},
"required": [
"sortBy"
],
"type": "object"
}
But I actually get the error : ts-json-schema-generator\dist\ts-json-schema-generator.js:30 throw error; ^
TypeError: Cannot read property ‘reduce’ of undefined.
Is there any possibility of supporting this kind of scenario?
Thanks! Rob
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (11 by maintainers)
Top Results From Across the Web
Documentation - Mapped Types - TypeScript
Generating mapping types which change properties via template literal strings. The TypeScript docs are an open source project. Help us improve these pages...
Read more >Mastering TypeScript mapped types - LogRocket Blog
In this post, we'll cover mapped types in TypeScript, a real-world example of them, and utility types including Partial, Readonly, and Pick.
Read more >Using 'Pick' in TypeScript to simplify Types and Interfaces
Mapped Types allow us to dynamically construct new types from existing ones, creating a new type with new meaning.
Read more >Mapped Types - OpenAPI - A progressive Node.js framework
Pick #. The PickType() function constructs a new type (class) by picking a set of properties from an input type. For example, suppose ......
Read more >Use TypeScript Mapped Types Like a Pro
Master TypeScript Mapped Types and understand how TypeScript's Utility Types ... Have you used the Partial, Required, Readonly, and Pick utility types?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in #ebad910
Well, I’ve got an ugly(?) implementation working for Partial. Will test and do a pull request.