fix: build errors by making getTranslation return type smarter

This commit is contained in:
Alessio Gravili
2024-03-18 15:48:03 -04:00
parent 602108b150
commit d08d04debd

View File

@@ -2,10 +2,12 @@ import type { JSX } from 'react'
import type { I18n } from '../types.js'
export const getTranslation = (
label: JSX.Element | Record<string, string> | string,
type LabelType = JSX.Element | Record<string, string> | string
export const getTranslation = <T extends LabelType>(
label: T,
i18n: Pick<I18n, 'fallbackLanguage' | 'language'>,
): JSX.Element | string => {
): T extends JSX.Element ? JSX.Element : string => {
// If it's a Record, look for translation. If string or React Element, pass through
if (typeof label === 'object' && !Object.prototype.hasOwnProperty.call(label, '$$typeof')) {
if (label[i18n.language]) {
@@ -24,5 +26,5 @@ export const getTranslation = (
}
// If it's a React Element or string, then we should just pass it through
return label as JSX.Element | string
return label as unknown as T extends JSX.Element ? JSX.Element : string
}