Mapped-type creating redundant intermediary definition (--aliasRefs)

Open
#293 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
tooling

Research direction

Start by running the TypeScript example with --aliasRefs and inspect the generated definitions. Trace mapped-type alias handling and compare the MappedType/$ref and __type output; done means the redundant __type intermediary is no longer emitted while the schema remains correct.

Written by the indexing model from the issue text.

Description

Possibly related to #232, although this seems more complicated than that.

Given the following TypeScript:

enum MyEnum { MY, AWESOME, ENUM }
type MappedType = { [key in MyEnum]?: string };

interface Type {
	map: MappedType;
}

The following schema is generated (with --aliasRefs):

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "MappedType": {
            "$ref": "#/definitions/__type"
        },
        "__type": {
            "properties": {
                "0": {
                    "type": "string"
                },
                "1": {
                    "type": "string"
                },
                "2": {
                    "type": "string"
                }
            },
            "type": "object"
        }
    },
    "properties": {
        "map": {
            "$ref": "#/definitions/MappedType"
        }
    },
    "type": "object"
}

As you can see there's a "intermediary" (and very much redundant) definitions created for the mapped type (__type). The actual MappedType definition references this definition.
No seperate enum definition is created, which makes sense because it's never truly referenced. If I do create an explicit reference the enum is generated correctly.

Dominant language
TypeScript
Stars
3.3k
Forks
332
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from YousefED/typescript-json-schema

All issues in YousefED/typescript-json-schema

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.