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

types: Rework TFunction and translation key suggestions #1898

Closed
wants to merge 1 commit into from

Conversation

kkuegler
Copy link
Member

@kkuegler kkuegler commented Jan 7, 2023

Allow 'other-ns:some.key' syntax regardless of current namespace(s). However, we no longer provide these keys via TS autosuggestion, to not flood the suggestion box with these.
This uses the 'DisableSuggestion' helper type, which unfortunately allows any capital case prefix to the actual 'other-ns:' prefix.

For getFixedT with multiple namespaces, all the keys in these namespaces are now suggested without 'ns:' prefix. Using such a prefix is still possible, though.

Technically, we treat cases with a fixed single namespace and multiple namespaces the same in TFuncKey by flattening an array of namespaces into a union of namespace strings. Using this we can use distributive conditional types to handle each namespace individually. See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types

For NormalizeReturn we use a new DistributiveKeyOf<T> helper. This finds keys that are available in only one/some of the namespaces, whereas 'keyof T' would only find them if they were in all of the namespaces.

We simplify and rework the "with options" overload of TFunction. This no longer allows passing the namespace. That use case is already covered by other overloads before that. This overload was the reason for erroneously suggesting unprefixed keys for not currently active namespaces.

The probably helps with #1887, too.
fixes #1889

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

Allow 'other-ns:some.key' syntax regardless of current namespace(s).
However, we no longer provide these keys via TS autosuggestion, to
not flood the suggestion box with these.
This uses the 'DisableSuggestion' helper type, which unfortunately
allows any capital case prefix to the actual 'other-ns:' prefix.

For getFixedT with multiple namespaces, all the keys in these namespaces
are now suggested without 'ns:' prefix. Using such a prefix is still possible,
though.

Technically, we treat cases with a fixed single namespace and multiple
namespaces the same in `TFuncKey` by flattening an array of namespaces
into a union of namespace strings. Using this we can use distributive
conditional types to handle each namespace individually. See
https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types

For `NormalizeReturn` we use a new `DistributiveKeyOf<T>` helper.
This finds keys that are available in only one/some of the namespaces,
whereas 'keyof T' would only find them if they were in all of the
namespaces.

We simplify and rework the "with options" overload of TFunction.
This no longer allows passing the namespace. That use case is already
covered by other overloads before that. This overload was the reason
for erroneously suggesting unprefixed keys for not currently active
namespaces.

The probably helps with i18next#1887, too.
fixes i18next#1889
@coveralls
Copy link

Coverage Status

Coverage: 91.95%. Remained the same when pulling 18c6274 on kkuegler:ns-prefix-1889 into 5bd9ac0 on i18next:master.

@stale
Copy link

stale bot commented Jan 16, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 16, 2023
@stale stale bot closed this Feb 2, 2023
@kkuegler kkuegler mentioned this pull request Feb 19, 2023
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getFixedT() types and namespace prefixes
4 participants