Use type information during translation key extraction
🚀 Feature Proposal
Resolve typescript types to support variables with literal types and union types of literal types being passed to the translation function as keys.
Motivation
i18next-parser has some known caveats. The need for the workaround is disappointing, as the purpose of the tool is to remove such manual maintenance, especially when the required information is available through typescript.
Example
The following keys a and b should be extracted from the following code:
let a: 'a' | 'b';
a = Math.random() > .5 ? 'a' : 'b';
i18n.t(a);
Implementation Notes
I implemented a proof of concept at mspiess/i18next-parser-ts.
Using type information requires access to the typeChecker which precludes the use of ts.createProgram instead of ts.createSourceFile.
createProgram admits a list of root file paths and walks the project from there.
Using it would require replacing the current vfs node stream architecture, which seems like a lot of work which would possibly incur some breaking changes, so I'd appreciate advice on the feasibility of such a major undertaking.
Maybe it's also possible to assemble a typeChecker without using createProgram. I don't know the typescript internals well enough to tell.
@mspiess I'm open to the idea but have no time to work on it myself. If yiou want to tackle this and open a PR, I will review it.