Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect output of Record with numeric key #1262

Closed
matt-pawley opened this issue May 25, 2022 · 2 comments
Closed

Incorrect output of Record with numeric key #1262

matt-pawley opened this issue May 25, 2022 · 2 comments

Comments

@matt-pawley
Copy link

matt-pawley commented May 25, 2022

A Record with a numeric key is incorrectly outputted as an array instead of an object.

// foo.ts

interface RecordValue {
  baz: 'baz';
}

export type Foo = Record<number, RecordValue>;

// Note, the following output as expected:

// export type Foo = Record<string, RecordValue>;

// export type Foo = {
//   [key: number]: RecordValue;
// };

Command:

ts-json-schema-generator --path 'foo.ts'

Actual Result:

{
  "$ref": "#/definitions/Foo",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Foo": {
      "items": {
        "additionalProperties": false,
        "properties": {
          "baz": {
            "const": "baz",
            "type": "string"
          }
        },
        "required": [
          "baz"
        ],
        "type": "object"
      },
      "type": "array"
    }
  }
}

Expected Result:

{
  "$ref": "#/definitions/Foo",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Foo": {
      "additionalProperties": {
        "additionalProperties": false,
        "properties": {
          "baz": {
            "const": "baz",
            "type": "string"
          }
        },
        "required": [
          "baz"
        ],
        "type": "object"
      },
      "type": "object"
    }
  }
}

Fully appreciate all object keys are strings in Javascript, so declaring a Record with a numeric interface is unusual. However. typescript allows it, so would be useful for ts-json-schema-generator to also support it.

@matt-pawley
Copy link
Author

Found the issue is in MappedTypeNodeParser - https://github.com/vega/ts-json-schema-generator/blob/next/src/NodeParser/MappedTypeNodeParser.ts#L57-L59

If we move keyListType instanceof NumberType into the above if statement, it works as expected. However this breaks https://github.com/vega/ts-json-schema-generator/blob/next/test/valid-data/type-mapped-array/main.ts

@matt-pawley
Copy link
Author

Fixed in v1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant