vojtechhabarta / typescript-generator

Generates TypeScript from Java - JSON declarations, REST service client
MIT License
1.15k stars 237 forks source link

Rename index to key for Map instances #776

Open pimfm opened 2 years ago

pimfm commented 2 years ago

Good day, really enjoying your generator, thanks for the ease of use of this project. My colleagues found a small thing that could be improved: This map

val explanations: Map<String, String>

is generated into this:

explanations: { [index: string]: string }

It would be really nice if this index name was actually 'key' to show the key/value relation.

I suspect this code has something to do with it, but am not familiar enough to offer you a pull reqest. https://github.com/vojtechhabarta/typescript-generator/blob/1d533d9466bfc1a79f31c5105408e5900281d96a/typescript-generator-core/src/main/resources/helpers/jsonDeserialization.ts#L10

Hope you find the space to do this name adjustment. Thanks in advance

vojtechhabarta commented 2 years ago

Hello, actually it is mainly here: https://github.com/vojtechhabarta/typescript-generator/blob/219c67120d92d6ca0da38955d6a2d86d41bd9703/typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TsType.java#L172-L187

This TypeScript feature is called Index Signatures (it was called Indexed Array in the past, I think) - https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures. That's why there is usually index. But I agree that for many people key would be more clear.

It would be easy change but I am not sure. There is also suggestion that Java Map should be generated as TypeScript Record (#706).