quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

TypeScript: Keys are not enforced to be Enums

Open mschli opened this issue 3 years ago • 0 comments

Hi there!

Thank you for your great tool!

Converting a dictonary-like JSON schema to TS types I have found the following issue:

The keys are not enforced as Enum but are only strings:

{
    "type": "object",
    "required": [
        "icon",
        "languages"
    ],
    "properties": {
        "icon": {
            "type": "string"
        },
        "languages": {
            "type": "object",
            "propertyNames": {
                "type": "string",
                "enum": ["de", "en"]
            },
            "additionalProperties": {
                "type": "object",
                "required": ["name"],
                "properties": {
                    "name": {
                        "type": "string",
                        "minLength": 1
                    },
                    "description": {
                        "type": "string"
                    },
                    "recommendation": {
                        "type": "string"
                    }
                }
            }
        }
    },
    "additionalProperties": false
}
export interface MySchema{
    icon:      string;
    languages: { [key: string]: Language }; // here I would expect the key to be of type enum de/en
}

export interface Language {
    description?:    string;
    name:            string;
    recommendation?: string;
}

I would consider this behaviour as a bug.

It get's worse if I put

"required": ["en"]

to my language property. Then it generates:

export interface MySchema{
    icon:      string;
    languages: Languages;
}

export interface Languages {
    en: Language;
}

mschli avatar Jun 10 '22 10:06 mschli