question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Pick and other Mapped Types

See original GitHub issue

Hi! 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:closed
  • Created 6 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
domoritzcommented, May 6, 2018

Fixed in #ebad910

1reaction
renewoollercommented, Mar 12, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found