Skip to content

Commit

Permalink
change function value type to generic function interface (#1163)
Browse files Browse the repository at this point in the history
* change function value type to generic function interface

* add modifier to my name

* add ts baseline test

* fix typo

* add ts baseline test

* fix typo

* run prettier prior to comparison with master

* use test setup from master

* run pretter with the same settings this time.

* adjusts TranslateFunction types in tests

* remove duplicated tests
  • Loading branch information
tkow authored and rosskevin committed Jan 16, 2019
1 parent 1f501ec commit 0c60262
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
7 changes: 5 additions & 2 deletions index.d.ts
Expand Up @@ -6,6 +6,7 @@
// Silas Rech <https://github.com/lenovouser>
// Philipp Katz <https://github.com/qqilihq>
// Milan Konir <https://github.com/butchyyyy>
// Takeo Kusama <https://github.com/tkow>

declare namespace i18next {
interface FallbackLngObjList {
Expand Down Expand Up @@ -459,11 +460,13 @@ declare namespace i18next {

type Callback = (error: any, t: TranslationFunction) => void

type TranslationFunction<
interface TranslationFunction<
TResult = any,
TValues extends object = object,
TKeys extends string = string
> = (key: TKeys | TKeys[], options?: TranslationOptions<TValues>) => TResult
> {
(key: TKeys | TKeys[], options?: TranslationOptions<TValues>): TResult
}

interface Resource {
[language: string]: ResourceLanguage
Expand Down
20 changes: 14 additions & 6 deletions test/typescript/i18next-tests.ts
Expand Up @@ -477,8 +477,6 @@ i18next.t('key2_interval', { postProcess: 'interval', count: 1 }) // -> "one ite
i18next.t('key2_interval', { postProcess: 'interval', count: 4 }) // -> "a few items"
i18next.t('key2_interval', { postProcess: 'interval', count: 100 }) // -> "100 items"
i18next.t('friend', { context: 'male', count: 1 }) // -> "A boyfriend"
i18next.t('friend', { context: 'male', count: 1 }) // -> "A boyfriend"
i18next.t('friend', { context: 'female', count: 100 }) // -> "100 girlfriends"
i18next.t('friend', { context: 'female', count: 100 }) // -> "100 girlfriends"
i18next.t('tree', { returnObjects: true, something: 'gold' })
// -> { res: 'added gold' }
Expand Down Expand Up @@ -507,12 +505,22 @@ i18next.t('friend')
i18next.t(['friend', 'tree'])
i18next.t('friend', { myVar: 'someValue' })
i18next.t(['friend', 'tree'], { myVar: 'someValue' })
i18next.t('friend', { myVar: 'someValue' })
i18next.t(['friend', 'tree'], { myVar: 'someValue' })

const t1: i18next.TranslationFunction = (key: string|string[], options?: i18next.TranslationOptions) => ''
const t2: i18next.TranslationFunction<{ value: string }> = (
key: string|string[],
options?: i18next.TranslationOptions,
) => ({ value: 'asd' })
const t3: i18next.TranslationFunction<string, CustomOptions> = (
key: string | string[],
options?: i18next.TranslationOptions<CustomOptions>,
) => ''
const t4: i18next.TranslationFunction<string, object, KeyList> = (
key: KeyList | KeyList[],
options?: i18next.TranslationOptions,
) => ''

i18next.exists('friend')
i18next.exists(['friend', 'tree'])
i18next.exists('friend', { myVar: 'someValue' })
i18next.exists(['friend', 'tree'], { myVar: 'someValue' })
i18next.exists('friend', { myVar: 'someValue' })
i18next.exists(['friend', 'tree'], { myVar: 'someValue' })

0 comments on commit 0c60262

Please sign in to comment.