Support string enums as keys in maps
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 267
- Avg merge
- 1d 25m
- Merged PRs (30d)
- 14
Description
## :rocket: Feature Request
### Affected Languages
- [X] `TypeScript` or `Javascript`
- [X] `Python`
- [X] `Java`
- [X] .NET (`C#`, `F#`, ...)
### General Information
* **JSII Version:** 1.7.0 (build 179a3a5), typescript 3.9.5
* **Platform:** Darwin $HOST 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64
* [ ] I may be able to implement this feature request
* [ ] This feature might incur a breaking change
### Description
Right now jsii only allows maps with string key. If instead a enum with strings is used the compile fails:
```
export enum CspDirectiveName {
CONNECT_SRC = "connect-src",
}
const cspDirectives: { [P in CspDirectiveName]?: string } = {};
// alternative
const cspDirectives: Partial> = { [CspDirectiveName.CONNECT_SRC] = "asdf"};
cspDirectives[CspDirectiveName.CONNECT_SRC] = "https://amazon.com"
```
error
```
error TS9999: JSII: Only string-indexed map types are supported
152 readonly cspDirectives: { [P in CspDirectiveName]?: string[] };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
### Proposed Solution
I'm actually not sure if that is really a jsii problem or more a problem in TypeScript which supports enum keys only with computed types. But I think it's a valid use-case for jsii as well.
Contributor guide
Research direction
Reproduce the TypeScript enum-keyed map example against jsii 1.7.0 and confirm the TS9999 error. Trace the compiler/type-validation entry point that rejects non-string-indexed maps, then define tests showing that string enum keys compile successfully and preserve the requested map behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java, javascript, python, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100