-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(types): correct types for macro with custom i18n instance #1141
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/lingui-js/js-lingui/9UdqQB48LVcvLhMmxiAfUUzPTdZ6 |
I'm not sure if this is something we want to have, but we could add https://github.com/Microsoft/dtslint and add assertions for correct/wrong usage of the macro, to avoid this in the future, but I'm not sure if it's useful, given that the only hand-written type definitions are in the macro anyway. |
Yep, just noticed the same. Created a playground on TypeScript website for checking this, I'm going to create a test-types.ts where I'll add some tests to macros types and some conflictive typings. There's one case where it fails: |
Codecov Report
@@ Coverage Diff @@
## main #1141 +/- ##
=======================================
Coverage 82.28% 82.28%
=======================================
Files 56 56
Lines 1699 1699
Branches 466 466
=======================================
Hits 1398 1398
Misses 176 176
Partials 125 125 Continue to review full report at Codecov.
|
Ah, never mind about the declare function t(i18n: I18n): (messageDescriptior: MessageDescriptor | TemplateStringsArray, ...placeholders: unknown[]) => string Take a look to the playground and tell me wdyt |
Ah nice catch, although I think the best way to do that is with overloads (so declaring the function multiple times. This disallows or errors when additional parameters are passed (placeholders, in this case, which doesn't make sense there). Do you want to also change any -> unknown? I don't think it makes any difference here, as those types are only for the implementation, not for consuming the passed arguments in Lingui itself. |
Oh btw, I'm not sure if |
Not sure too, I'll check it tomorrow in detail and we adjust the typing accordingly, there's no hurry at all. |
I think I have a fix for both the types, and adding support for |
7ac9b04
to
bab91ae
Compare
Better now, example playground here |
Thanks again! 👍 |
Thanks to you 🙏 |
This PR fixes an issue introduced in #1139, that would cause a type error to be raised, even if the usage was correct.
I'm not sure how I didn't notice this before, but I guess it's because either type checking was disabled while I tested it, or because the files were JS only.
The issue is that for TypeScript, doing
t(x)`template ${string}`;
, is seen as two individual calls (result = t(x)
andresult`template ${string}`;
), so it also needs to be typed like that, even though the macro itself will never let it be executed as such.