Skip to content
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

Merged
merged 2 commits into from
Sep 28, 2021

Conversation

jeroenvisser101
Copy link
Contributor

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) and result`template ${string}`;), so it also needs to be typed like that, even though the macro itself will never let it be executed as such.

@vercel
Copy link

vercel bot commented Sep 28, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/lingui-js/js-lingui/9UdqQB48LVcvLhMmxiAfUUzPTdZ6
✅ Preview: https://js-lingui-git-fork-jeroenvisser101-jv-fix-type-f644b0-lingui-js.vercel.app

@jeroenvisser101
Copy link
Contributor Author

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.

@semoal
Copy link
Contributor

semoal commented Sep 28, 2021

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: t(i18n)(messageDescriptor), basically this use case is when using defineMessage and the custom instance of i18n (it should work I guess)

Playground

@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #1141 (c222772) into main (8921156) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           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.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 47aa68a...c222772. Read the comment docs.

@semoal
Copy link
Contributor

semoal commented Sep 28, 2021

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: t(i18n)(messageDescriptor), basically this use case is when using defineMessage and the custom instance of i18n (it should work I guess)

Playground

Ah, never mind about the messageDescriptor issue, I think we can fix it like this:

declare function t(i18n: I18n): (messageDescriptior: MessageDescriptor | TemplateStringsArray, ...placeholders: unknown[]) => string

Take a look to the playground and tell me wdyt

@jeroenvisser101
Copy link
Contributor Author

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.

@jeroenvisser101
Copy link
Contributor Author

Oh btw, I'm not sure if t(i18n)(messageDescriptor) works, I didn't implement it as such, so if it works, it's by accident

@semoal
Copy link
Contributor

semoal commented Sep 28, 2021

Oh btw, I'm not sure if t(i18n)(messageDescriptor) works, I didn't implement it as such, so if it works, it's by accident

Not sure too, I'll check it tomorrow in detail and we adjust the typing accordingly, there's no hurry at all.

@jeroenvisser101
Copy link
Contributor Author

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 t(i18n)(messageDescriptor) (and a small typo fix in the argument name in the types), no rush though :)

@jeroenvisser101
Copy link
Contributor Author

Better now, example playground here

@semoal semoal merged commit a9bffdd into lingui:main Sep 28, 2021
@jeroenvisser101
Copy link
Contributor Author

Thanks again! 👍

@semoal
Copy link
Contributor

semoal commented Sep 28, 2021

Thanks again! 👍

Thanks to you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants