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 #1299: add support for icu macro in typescript #1310

Merged
merged 2 commits into from
May 10, 2021

Conversation

cellog
Copy link
Contributor

@cellog cellog commented May 1, 2021

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • documentation is changed or added

@coveralls
Copy link

coveralls commented May 1, 2021

Coverage Status

Coverage remained the same at 96.222% when pulling 40dc40b on cellog:icu-types into da07fba on i18next:master.

@pedrodurek pedrodurek self-requested a review May 2, 2021 13:37
@cellog cellog changed the title fix #17: add support for icu macro in typescript fix #1299: add support for icu macro in typescript May 6, 2021
ts4.1/icu.d.ts Outdated
Comment on lines 1 to 6
declare module 'react-i18next/icu.macro' {
type Namespace = import('.').Namespace;
type DefaultNamespace = import('.').DefaultNamespace;
type Resources = import('.').Resources;
type TFuncKey<N extends Namespace = DefaultNamespace, T = Resources> = import('.').TFuncKey<N, T>;
type i18n = import('i18next').i18n;
Copy link
Member

@pedrodurek pedrodurek May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything inside the module will be exported (even without the export keyword), I don't think we have an i18n under react-i18next. If you don't want to re-export all these types, you should import them outside the module.

Suggested change
declare module 'react-i18next/icu.macro' {
type Namespace = import('.').Namespace;
type DefaultNamespace = import('.').DefaultNamespace;
type Resources = import('.').Resources;
type TFuncKey<N extends Namespace = DefaultNamespace, T = Resources> = import('.').TFuncKey<N, T>;
type i18n = import('i18next').i18n;
import {Namespace, DefaultNamespace, Resources, TFuncKey, TransProps} from '.';
import {i18n} from 'i18next';
declare module 'react-i18next/icu.macro' {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I did it this way, the types stopped working in VS Code, and it also failed to type them at all (they became any). Can you confirm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it works perfectly, but you also need to rename the file declaration to uci.macro.d.ts, otherwise, it won't find the module.

Screenshot 2021-05-08 at 1 14 01 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, tslint is going nuts now. Should we be concerned? I'm going to push this as you recommend, and then we can decide this question.

Screen Shot 2021-05-08 at 9 09 15 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can add the export again if you want

ts4.1/icu.d.ts Outdated
K extends TFuncKey<N> extends infer A ? A : never,
N extends Namespace = DefaultNamespace
> {
children?: never;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can simply remove children

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent here was to prevent children. In other words, typescript will error if you pass children in, as a way of signaling "children will be ignored"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will happen for Select component only 🤔 , because you're intersecting its props with SelectSubProps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I'll add it to PluralProps to solve that oversight

ts4.1/icu.d.ts Outdated
type TFuncKey<N extends Namespace = DefaultNamespace, T = Resources> = import('.').TFuncKey<N, T>;
type i18n = import('i18next').i18n;

export interface PluralSubProps<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned above all types inside the module will be exported so we don't need the export keyword

ts4.1/icu.d.ts Outdated
Comment on lines 80 to 85
export function Trans<
K extends TFuncKey<N> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
E extends Element = HTMLDivElement
>(props: import('.').TransProps<K, N, E>): React.ReactElement;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can re-export the Trans function, but it needs to be done outside the module.

Suggested change
export function Trans<
K extends TFuncKey<N> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
E extends Element = HTMLDivElement
>(props: import('.').TransProps<K, N, E>): React.ReactElement;
}
}
export { Trans };

@cellog cellog requested a review from pedrodurek May 9, 2021 01:11
Copy link
Member

@pedrodurek pedrodurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!! 🎉 🙌

@cellog
Copy link
Contributor Author

cellog commented May 9, 2021

awesome! I'm going to put up the other PR idea in a draft PR. I will rebase it on master once this is merged, so I can add the typescript types for it too. Nice doing bidness wit you :)

@jamuhl jamuhl merged commit 756f34f into i18next:master May 10, 2021
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.

5 participants