toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
627 stars 101 forks source link

Generated key typings creates unsupported keys #582

Open V-ed opened 9 months ago

V-ed commented 9 months ago

Describe the bug

When using the typesOutputPath option, the package generates a file containing a Record of (abstracted for this issue) type KeyMap = Record<string, string | KeyMap> which in itself is working fine.

The issue stems from how the key paths are generated, which creates keys with "sub"-keys that are unusable ('admin' | 'admin.labels'| [...]). See picture below for a clearer picture on what I'm trying to say :

image In the example above, only 'admin.emails.register.body.cta-button' and 'admin.emails.register.body.greetings' should be shown (this example is trimmed down), as all the keys above them are unusable keys that are generated via the export type Path and its sister types PathImpl2 and PathImpl as inlined below :

https://github.com/toonvanstrijp/nestjs-i18n/blob/6b1bb3ad2883822015971e98731e8fb6cfff7919/src/types.ts#L27-L33

This makes it awkward to use the generated keys, and I've barely started translating my app I'm already having way too many entries in the intellisense (so much that I'm currently disabling the generated typings option in favour of a vscode extension, which makes my app error-prone at typescript's compile time).

I believe this especially happens with nested keys (see example below), but I could be wrong on this as I haven't tested every single scenario yet.

image

Reproduction

Even the starter example with few nested keys does it

System Info

System:
    OS: Linux 5.10 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 247.43 MB / 3.83 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 18.17.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.8.1 - /usr/local/bin/npm
    pnpm: 8.8.0 - /usr/local/share/npm-global/bin/pnpm

Used Package Manager

pnpm

Validations