Skip to content

Commit d7363bb

Browse files
change types to allow only accepted combinations of arguments
1 parent e560cfa commit d7363bb

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/i18n/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { I18nStructure, I18n, Substitution } from './types';
55
import { browser } from '@wxt-dev/browser';
66

77
export function createI18n<T extends I18nStructure>(): I18n<T> {
8-
const t = ((key, ...args) => {
8+
const t = ((key: string, ...args: unknown[]) => {
99
// Resolve args
1010
let sub: Substitution[] | undefined;
1111
let count: number | undefined;

packages/i18n/src/types.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ export type I18nStructure = {
99

1010
type DefaultTFunction<TKeys extends string> = {
1111
(key: TKeys): string;
12-
(key: TKeys, substitutions?: string[]): string;
12+
(key: TKeys, substitutions: string[]): string;
1313
(key: TKeys, n: number): string;
14-
(key: TKeys, n: number, substitutions?: string[]): string;
14+
(key: TKeys, n: number, substitutions: string[]): string;
1515
};
1616

1717
export interface I18n<T extends I18nStructure> {
18-
t: T extends I18nStructure
19-
? DefaultTFunction<keyof T & string>
20-
: TFunction<Extract<T, I18nStructure>>;
18+
t: string extends keyof T ? DefaultTFunction<keyof T & string> : TFunction<T>;
2119
}
2220

2321
// prettier-ignore
@@ -41,10 +39,24 @@ export type TFunction<T extends I18nStructure> = {
4139
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: 0 } ? P : never; }[keyof T],
4240
): string;
4341

44-
// Non-plural with optional substitutions
42+
// Non-plural with 1 substitution
4543
<K extends keyof T>(
4644
// prettier-ignore
47-
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: SubstitutionCount } ? P : never; }[keyof T],
45+
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: 1 } ? P : never; }[keyof T],
46+
substitutions: SubstitutionTuple<1>,
47+
): string;
48+
49+
// Non-plural with 2 substitutions
50+
<K extends keyof T>(
51+
// prettier-ignore
52+
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: 2 } ? P : never; }[keyof T],
53+
substitutions: SubstitutionTuple<2>,
54+
): string;
55+
56+
// Non-plural with 3+ substitutions
57+
<K extends keyof T>(
58+
// prettier-ignore
59+
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: 3 | 4 | 5 | 6 | 7 | 8 | 9 } ? P : never; }[keyof T],
4860
substitutions: T[K] extends I18nFeatures
4961
? SubstitutionTuple<T[K]['substitutions']>
5062
: never,

0 commit comments

Comments
 (0)