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

TypeScript: Keys are not enforced to be Enums #1959

Open
mschli opened this issue Jun 10, 2022 · 0 comments
Open

TypeScript: Keys are not enforced to be Enums #1959

mschli opened this issue Jun 10, 2022 · 0 comments
Labels
bug input:JSON Schema JSON Schema Input TypeScript TypeScript Output

Comments

@mschli
Copy link

mschli commented Jun 10, 2022

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;
}
@inferrinizzard inferrinizzard added bug TypeScript TypeScript Output input:JSON Schema JSON Schema Input labels May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug input:JSON Schema JSON Schema Input TypeScript TypeScript Output
Projects
None yet
Development

No branches or pull requests

2 participants