Skip to content

Commit

Permalink
Remove explicit type restriction on Translate generic (#1013)
Browse files Browse the repository at this point in the history
Co-authored-by: zsunderland <[email protected]>
  • Loading branch information
SimplyComplexable and zsunderland authored Mar 28, 2023
1 parent d3ae57a commit bc40466
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
11 changes: 1 addition & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ export interface TranslationQuery {
[name: string]: any
}


export interface NestedStringObject {
[key:string]: string | NestedStringObject | NestedStringArray
}
type ValueOrArray<T> = T | ValueOrArray<T>[];
type NestedStringArray = ValueOrArray<string | NestedStringObject>;

export type TranslateValue = string | NestedStringObject | NestedStringArray

export type Translate = <T extends TranslateValue = string>(
export type Translate = <T extends unknown = string>(
i18nKey: string | TemplateStringsArray,
query?: TranslationQuery | null,
options?: {
Expand Down
12 changes: 7 additions & 5 deletions src/transCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
I18nDictionary,
LoaderConfig,
LoggerProps,
TranslateValue,
TranslationQuery,
} from '.'
import { Translate } from './index'
Expand Down Expand Up @@ -38,9 +37,12 @@ export default function transCore({
allowEmptyStrings = true,
} = config

const interpolateUnknown = (value: TranslateValue, query?: TranslationQuery | null): TranslateValue => {
const interpolateUnknown = (
value: unknown,
query?: TranslationQuery | null
): typeof value => {
if (Array.isArray(value)) {
return value.map(val => interpolateUnknown(val, query));
return value.map((val) => interpolateUnknown(val, query))
}
if (value instanceof Object) {
return objectInterpolation({
Expand Down Expand Up @@ -124,7 +126,7 @@ function getDicValue(
options: { returnObjects?: boolean; fallback?: string | string[] } = {
returnObjects: false,
}
): TranslateValue | undefined {
): unknown | undefined {
const { keySeparator = '.' } = config || {}
const keyParts = keySeparator ? key.split(keySeparator) : [key]

Expand All @@ -148,7 +150,7 @@ function getDicValue(
typeof value === 'string' ||
((value as unknown) instanceof Object && options.returnObjects)
) {
return value as TranslateValue
return value
}

return undefined
Expand Down

0 comments on commit bc40466

Please sign in to comment.