glideapps / glideapps/quicktype

TypeScript: Keys are not enforced to be Enums

Open
#1,959 0 comments 2 reactions 0 assignees View on GitHub
bug input:JSON Schema TypeScript
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

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;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.