fix: add generic to LabelFunction to prevent type error for custom translation keys (#9335)

When creating custom translations and merging the custom translation
keys with the default translation keys, and then pass it to the generic
TFunction type, Typescript complains that the function does not satisfy
the LabelFunction type in a label field.

The reason for this is that the LabelFunction type is not generic, and
it's always using the default TFunction which itself uses the
DefaultTranslationKey if no type is passed to it.

This is solved by making the LabelFunction generic and forward the
TTranslationKeys to the TFunction type.

Following this documentation:
https://payloadcms.com/docs/configuration/i18n#typescript 

Example:

![image](https://github.com/user-attachments/assets/583ae9ca-63e7-4f49-9ded-0fca0a4a3d80)
This commit is contained in:
Marwin Hormiz
2024-11-28 22:48:49 +01:00
committed by GitHub
parent 3e4f7dbae2
commit 27eeac2568

View File

@@ -1,4 +1,5 @@
import type {
DefaultTranslationKeys,
DefaultTranslationsObject,
I18nClient,
I18nOptions,
@@ -493,7 +494,11 @@ export type LocalizationConfig = Prettify<
LocalizationConfigWithLabels | LocalizationConfigWithNoLabels
>
export type LabelFunction = ({ t }: { t: TFunction }) => string
export type LabelFunction<TTranslationKeys = DefaultTranslationKeys> = ({
t,
}: {
t: TFunction<TTranslationKeys>
}) => string
export type StaticLabel = Record<string, string> | string