-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Argument with custom type is not passed to the formatter function #747
Comments
I've stumbled upon this as well. // en/component/index.ts
totalPrice: '{value:Currency|currency}', // custom-types.ts
export type Currency = { value: number; currency: string } // formatters.ts
currency: (value: Currency) =>
number(locale, {
style: 'currency',
currency: value.currency,
maximumFractionDigits: 2,
})(value.value) |
I also got hit with this - and also with a currency formatter similar to @vladev. // i18n/custom-types.ts
export type MonetaryAmount = {
currency: string
value: number
}
// i18n/en/index.ts
const en = {
money: "{0:MonetaryAmount|currency}",
}
// i18n/formatters.ts
const formatters: Formatters = {
currency: ({ value, currency }: MonetaryAmount): string => {
const formatter = new Intl.NumberFormat(locale, {
style: "currency",
currency,
minimumFractionDigits: 0,
}
return formatter.format(value)
}
// component.ts
function Money({value, valueCurrency}: {value: number, valueCurrency: string}) {
const { LL } = useI18nContext()
// below is correctly typed, but throws TypeError: Cannot read property 'value' of undefined
return <Text>{LL.money({ value, currency: valueCurrency })}</Text>
} Even though this was passing typescript, the call to As a workaround, specifying an argument key in the translation passes typescript and works as expected at runtime: // i18n/en/index.ts
const en = {
money: "{money:MonetaryAmount|currency}",
}
// component.ts
function Money({value, valueCurrency}: {value: number, valueCurrency: string}) {
const { LL } = useI18nContext()
// now compiles in typescript and works as expected at runtime
return <Text>{LL.money({ money: { value, currency: valueCurrency }})}</Text>
} So this is only an issue if you use a single indexed argument in the translation. |
Version
5.26.2
Describe the bug
Following this instruction failed to pass an argument of custom type to the formatter function.
Reproduction
Logs
Value correctly typed but the value undefined
Config
Additional information
No response
The text was updated successfully, but these errors were encountered: