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

formatString() TypeScript type specifies Array as a return value #139

Open
peterbasar opened this issue Nov 27, 2023 · 4 comments
Open

formatString() TypeScript type specifies Array as a return value #139

peterbasar opened this issue Nov 27, 2023 · 4 comments

Comments

@peterbasar
Copy link

Hello, thanks for making this package. I enjoy using it.
However, I encountered a potential issue while using it with TypeScript.
It seems that formatString() return type specifies an Array as well as string.
Shouldn't it only be a string? Since even docs specify that it returns a string.

formatString() - formats the input string and returns a new string, replacing its placeholders with the other arguments strings

See the location of the type and commit in question:
8a700b2#r133591646

Seems that this the potential issue has been introduced in:
f3f7385

@stefalda
Copy link
Owner

Hi, I have to review the code, because it seems to me that that change was required to be able to intermix JSX code in the returned string, I don't remember if the result is an array and so the tpe definition had to be modified ...

@peterbasar
Copy link
Author

Do you think it is a good decision to have both JSX and string formatting for 1 function?
It generally doesn't matter if the output is used directly in the renderer. But when the formatString output is used as an function argument there will have to be additional checking on every use.

@stefalda
Copy link
Owner

The JSX interpolation wasn't an option at start, it was added in a second time... but if we create a separate function we break existing uses..

@michaeltnguyen
Copy link

It's a hack, but you can override this behavior in your project by extending the LocalizedStringsMethods type.

// copied from type definition
// https://github.com/stefalda/react-localization/blob/master/lib/LocalizedStrings.d.ts
type Formatted = number | string | JSX.Element;
type FormatObject<U extends Formatted> = { [key: string]: U };

interface FixedLocalizedStringsMethods extends LocalizedStringsMethods {
  formatString<T extends Formatted>(
    str: string,
    ...values: Array<T | FormatObject<T>>
  ): string;
}

// now strings.formatString just returns a string
export type strings = FixedLocalizedStringsMethods & ILocalizedStrings;

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

No branches or pull requests

3 participants