File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,14 +35,16 @@ describe('I18n Types', () => {
3535 } ) ;
3636
3737 describe ( 'With type-safety' , ( ) => {
38- const i18n = createI18n < {
38+ type MyStructure = {
3939 simple : { plural : false ; substitutions : 0 } ;
4040 simpleSub1 : { plural : false ; substitutions : 1 } ;
4141 simpleSub2 : { plural : false ; substitutions : 2 } ;
4242 plural : { plural : true ; substitutions : 0 } ;
4343 pluralSub1 : { plural : true ; substitutions : 1 } ;
4444 pluralSub2 : { plural : true ; substitutions : 2 } ;
45- } > ( ) ;
45+ } ;
46+
47+ const i18n = createI18n < MyStructure > ( ) ;
4648
4749 describe ( 't' , ( ) => {
4850 it ( 'should only allow passing valid combinations of arguments' , ( ) => {
Original file line number Diff line number Diff line change 11/** @module @wxt -dev/i18n */
2- import {
3- I18nStructure ,
4- DefaultI18nStructure ,
5- I18n ,
6- Substitution ,
7- } from './types' ;
2+ import { I18nStructure , I18n , Substitution , UntypedI18n } from './types' ;
83import { browser } from '@wxt-dev/browser' ;
94
10- export function createI18n <
11- T extends I18nStructure = DefaultI18nStructure ,
12- > ( ) : I18n < T > {
13- const t = ( key : string , ...args : any [ ] ) => {
5+ export function createI18n ( ) : UntypedI18n ;
6+ export function createI18n < T extends I18nStructure > ( ) : I18n < T > ;
7+ export function createI18n ( ) : UntypedI18n {
8+ const t : UntypedI18n [ 't' ] = ( key : string , ...args : unknown [ ] ) => {
149 // Resolve args
1510 let sub : Substitution [ ] | undefined ;
1611 let count : number | undefined ;
@@ -64,5 +59,5 @@ export function createI18n<
6459 }
6560 } ;
6661
67- return { t } as I18n < T > ;
62+ return { t } ;
6863}
Original file line number Diff line number Diff line change @@ -3,14 +3,23 @@ export interface I18nFeatures {
33 substitutions : SubstitutionCount ;
44}
55
6- export type I18nStructure = {
7- [ K : string ] : I18nFeatures ;
8- } ;
6+ export interface UntypedI18n {
7+ t : UntypedTFunction ;
8+ }
99
10- export type DefaultI18nStructure = {
11- [ K : string ] : any ;
10+ export type UntypedTFunction = {
11+ ( key : string ) : string ;
12+ ( key : string , substitutions : Substitution [ ] ) : string ;
13+ ( key : string , n : number ) : string ;
14+ ( key : string , n : number , substitutions : Substitution [ ] ) : string ;
1215} ;
1316
17+ export type I18nStructure = Record < string , I18nFeatures > ;
18+
19+ export interface I18n < T extends I18nStructure > {
20+ t : TFunction < T > ;
21+ }
22+
1423// prettier-ignore
1524export type SubstitutionTuple < T extends SubstitutionCount > =
1625 T extends 1 ? [ $1 : Substitution ]
@@ -66,10 +75,6 @@ export type TFunction<T extends I18nStructure> = {
6675 ) : string ;
6776} ;
6877
69- export interface I18n < T extends DefaultI18nStructure > {
70- t : TFunction < T > ;
71- }
72-
7378export type Substitution = string | number ;
7479
7580type SubstitutionCount = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
You can’t perform that action at this time.
0 commit comments