[BUG]: (?) Incorrect union when literals overlap
Issue Type
Quicktype output
Context (Environment, Version, Language)
- Input format: JSON
- Target language: TypeScript
- Quicktype version: 23.0.170
- Quicktype usage: CLI
Description
I have JSON files containing NHL roster data. Each player object has a positionCode property and a shootsCatches property:
- The
positionCodeproperty can be one of"L","C","R","D", or"G". - The
shootsCatchesproperty can only be"L"or"R".
It's important to note here that "L" and "R" overlap between these two properties.
When I convert the JSON to TypeScript using Quicktype, the resulting type for the positionCode property is:
export type PositionCode = "L" | "C" | "R" | "D" | "G";
This part is correct, as it reflects all possible values for positionCode. However, Quicktype incorrectly assigns the same union type to the shootsCatches property, which should only allow "L" or "R".
Input Data
To generate the input data:
- Create a folder called
jsoninside the root of a Node.js project. - Run this script from the project root:
import fs from "fs";
import path from "path";
const SAMPLES = 50;
const PLAYERS_PER_TEAM = 30;
const POSITION_CODES = ["L", "C", "R", "D", "G"];
const SHOOTS_CATCHES = ["L", "R"];
const basePath = path.join(process.cwd(), "json");
for (let sample = 0; sample < SAMPLES; sample++) {
const players = [];
for (let player = 0; player < PLAYERS_PER_TEAM; player++) {
players.push({
positionCode: POSITION_CODES[player % POSITION_CODES.length],
shootsCatches: SHOOTS_CATCHES[player % SHOOTS_CATCHES.length],
});
}
fs.writeFileSync(
path.join(basePath, `${sample}.json`),
JSON.stringify({
players,
})
);
}
Command to generate output
quicktype json/*.json --top-level Root --lang typescript --just-types --prefer-types --prefer-const-values --prefer-unions -o generated-types.ts
Expected Behaviour / Output
I expect positionCode and shootsCatches to be seperate union types.
export type Root = {
players: Player[];
}
export type Player = {
positionCode: PositionCode;
shootsCatches: ShootsCatches;
}
export type PositionCode = "L" | "C" | "R" | "D" | "G";
export type ShootsCatches = "L" | "R";
Current Behaviour / Output
export type Root = {
players: Player[];
}
export type Player = {
positionCode: PositionCode;
shootsCatches: PositionCode;
}
export type PositionCode = "L" | "C" | "R" | "D" | "G";