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

Typing of 't' function when used as parameter inside a function #500

Closed
fitimbytyqi opened this issue Sep 6, 2023 · 5 comments
Closed
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@fitimbytyqi
Copy link

fitimbytyqi commented Sep 6, 2023

Is your feature request related to a problem? Please describe.

Currently, I have a function that accepts the t function from useTranslations.

e.x const columns = (t: 'some type') => [...data];

The type of t was a bit difficult to find because it's not exported from the package as far as am aware?

Describe the solution you'd like

What I would want to have is an exported type from the package which is easier to be found for beginners of this package.

e.x const columns = (t: TFunction) => [...data];

Describe alternatives you've considered

Right now, I have created my own interface which extends from the package which looks like this.

I can also make a pull request if needed for this solution, please let me know ;)

export interface TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never
> {
  <
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >
 ... more lines ....
@fitimbytyqi fitimbytyqi added enhancement New feature or request unconfirmed Needs triage. labels Sep 6, 2023
@amannn
Copy link
Owner

amannn commented Sep 7, 2023

This is currently not supported by design, please see these previous discussions:

@amannn amannn closed this as completed Sep 7, 2023
@adarshaacharya
Copy link

adarshaacharya commented Sep 17, 2024

export interface TFunction<
NestedKey extends NamespaceKeys<
IntlMessages,
NestedKeyOf

= never
{
<
TargetKey extends MessageKeys<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}
>,
NestedKeyOf<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}
>
>
>

@fitimbytyqi can you share your whole interface of this.

export type TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never,
> = ReturnType<
  typeof useTranslations<
    NestedKey extends never ? NestedKeyOf<IntlMessages> : NestedKey
  >
>;

Here's what I'm using currently but its not that good.

@fitimbytyqi
Copy link
Author

export interface TFunction<
NestedKey extends NamespaceKeys<
IntlMessages,
NestedKeyOf

= never
{
<
TargetKey extends MessageKeys<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}

,
NestedKeyOf<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}

@fitimbytyqi can you share your whole interface of this.

export type TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never,
> = ReturnType<
  typeof useTranslations<
    NestedKey extends never ? NestedKeyOf<IntlMessages> : NestedKey
  >
>;

Here's what I'm using currently but its not that good.

Sure, here is the long version of it.

import { ReactElement, ReactNode } from 'react';
import {
  Formats,
  MessageKeys,
  NamespaceKeys,
  NestedKeyOf,
  NestedValueOf,
  RichTranslationValues,
  TranslationValues,
} from 'next-intl';

export interface TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never,
> {
  <
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey,
    values?: TranslationValues,
    formats?: Partial<Formats>
  ): string;
  rich<
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey,
    values?: RichTranslationValues,
    formats?: Partial<Formats>
  ): string | ReactElement | ReactNode;
  raw<
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey
  ): any;
}

@adarshaacharya
Copy link

export interface TFunction<
NestedKey extends NamespaceKeys<
IntlMessages,
NestedKeyOf

= never
{
<
TargetKey extends MessageKeys<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}

,
NestedKeyOf<
NestedValueOf<
{
'!': IntlMessages;
},
[NestedKey] extends [never] ? '!' : !.${NestedKey}

@fitimbytyqi can you share your whole interface of this.

export type TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never,
> = ReturnType<
  typeof useTranslations<
    NestedKey extends never ? NestedKeyOf<IntlMessages> : NestedKey
  >
>;

Here's what I'm using currently but its not that good.

Sure, here is the long version of it.

import { ReactElement, ReactNode } from 'react';
import {
  Formats,
  MessageKeys,
  NamespaceKeys,
  NestedKeyOf,
  NestedValueOf,
  RichTranslationValues,
  TranslationValues,
} from 'next-intl';

export interface TFunction<
  NestedKey extends NamespaceKeys<
    IntlMessages,
    NestedKeyOf<IntlMessages>
  > = never,
> {
  <
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey,
    values?: TranslationValues,
    formats?: Partial<Formats>
  ): string;
  rich<
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey,
    values?: RichTranslationValues,
    formats?: Partial<Formats>
  ): string | ReactElement | ReactNode;
  raw<
    TargetKey extends MessageKeys<
      NestedValueOf<
        {
          '!': IntlMessages;
        },
        [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
      >,
      NestedKeyOf<
        NestedValueOf<
          {
            '!': IntlMessages;
          },
          [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
        >
      >
    >,
  >(
    key: TargetKey
  ): any;
}

thanks !

I think this type utility should be exported from library itself, very useful and typesafe.

@fitimbytyqi
Copy link
Author

@adarshaacharya You are welcome, that would be nice but they don't plan on doing it, so we can use this as an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

3 participants