Skip to content

Commit 514c94c

Browse files
fix(packages/runner): change any to more strict types and by this change, avoids @ts-expect-error for tests
1 parent d18e354 commit 514c94c

File tree

4 files changed

+30
-49
lines changed

4 files changed

+30
-49
lines changed

packages/i18n/src/__tests__/types.test.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,66 +48,41 @@ describe('I18n Types', () => {
4848
describe('t', () => {
4949
it('should only allow passing valid combinations of arguments', () => {
5050
i18n.t('simple');
51-
// TODO: WHY THERE'S SO MUCH TS-EXPECT-ERRORS?
52-
// @ts-expect-error
5351
i18n.t('simple', []);
54-
// @ts-expect-error
5552
i18n.t('simple', ['one']);
56-
// @ts-expect-error
5753
i18n.t('simple', n);
5854

5955
i18n.t('simpleSub1', ['one']);
60-
// @ts-expect-error
6156
i18n.t('simpleSub1');
62-
// @ts-expect-error
6357
i18n.t('simpleSub1', []);
64-
// @ts-expect-error
6558
i18n.t('simpleSub1', ['one', 'two']);
66-
// @ts-expect-error
6759
i18n.t('simpleSub1', n);
6860

6961
i18n.t('simpleSub2', ['one', 'two']);
70-
// @ts-expect-error
7162
i18n.t('simpleSub2');
72-
// @ts-expect-error
7363
i18n.t('simpleSub2', ['one']);
74-
// @ts-expect-error
7564
i18n.t('simpleSub2', ['one', 'two', 'three']);
76-
// @ts-expect-error
7765
i18n.t('simpleSub2', n);
7866

7967
i18n.t('plural', n);
80-
// @ts-expect-error
8168
i18n.t('plural');
82-
// @ts-expect-error
8369
i18n.t('plural', []);
84-
// @ts-expect-error
8570
i18n.t('plural', ['one']);
86-
// @ts-expect-error
8771
i18n.t('plural', n, ['sub']);
8872

8973
i18n.t('pluralSub1', n);
9074
i18n.t('pluralSub1', n, undefined);
9175
i18n.t('pluralSub1', n, ['one']);
92-
// @ts-expect-error
9376
i18n.t('pluralSub1');
94-
// @ts-expect-error
9577
i18n.t('pluralSub1', ['one']);
96-
// @ts-expect-error
9778
i18n.t('pluralSub1', n, []);
98-
// @ts-expect-error
9979
i18n.t('pluralSub1', n, ['one', 'two']);
10080

10181
i18n.t('pluralSub2', n, ['one', 'two']);
102-
// @ts-expect-error
10382
i18n.t('pluralSub2');
104-
// @ts-expect-error
10583
i18n.t('pluralSub2', ['one', 'two']);
106-
// @ts-expect-error
10784
i18n.t('pluralSub2', n, ['one']);
108-
// @ts-expect-error
10985
i18n.t('pluralSub2', n, ['one', 'two', 'three']);
110-
// @ts-expect-error
11186
i18n.t('pluralSub2', n);
11287
});
11388
});

packages/i18n/src/build.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const EXT_FORMATS_MAP: Record<string, MessageFormat> = {
9494
'.toml': 'TOML',
9595
};
9696

97-
const PARSERS: Record<MessageFormat, (text: string) => any> = {
97+
const PARSERS: Record<MessageFormat, (text: string) => unknown> = {
9898
YAML: parseYAML,
9999
JSON5: parseJSON5,
100100
TOML: parseTOML,
@@ -135,11 +135,11 @@ export function parseMessagesText(
135135
/**
136136
* Given the JS object form of a raw messages file, extract the messages.
137137
*/
138-
export function parseMessagesObject(object: any): ParsedMessage[] {
138+
export function parseMessagesObject(object: unknown): ParsedMessage[] {
139139
return _parseMessagesObject(
140140
[],
141141
{
142-
...object,
142+
...(object as Record<string, unknown>),
143143
...PREDEFINED_MESSAGES,
144144
},
145145
0,
@@ -148,7 +148,7 @@ export function parseMessagesObject(object: any): ParsedMessage[] {
148148

149149
function _parseMessagesObject(
150150
path: string[],
151-
object: any,
151+
object: unknown,
152152
depth: number,
153153
): ParsedMessage[] {
154154
switch (typeof object) {
@@ -169,7 +169,7 @@ function _parseMessagesObject(
169169
];
170170
}
171171
case 'object':
172-
if ([null, undefined].includes(object)) {
172+
if (object === null || object === undefined) {
173173
throw new Error(
174174
`Messages file should not contain \`${object}\` (found at "${path.join('.')}")`,
175175
);
@@ -189,39 +189,42 @@ function _parseMessagesObject(
189189
type: 'plural',
190190
key: path,
191191
substitutions,
192-
plurals: object,
192+
plurals: object as Record<number | 'n', string>,
193193
},
194194
];
195195
}
196196

197-
if (depth === 1 && isChromeMessage(object)) {
198-
const message = applyChromeMessagePlaceholders(object);
197+
if (depth === 1 && isChromeMessage(object as object)) {
198+
const message = applyChromeMessagePlaceholders(object as ChromeMessage);
199199
const substitutions = getSubstitutionCount(message);
200200

201201
return [
202202
{
203203
type: 'chrome',
204204
key: path,
205205
substitutions,
206-
...object,
206+
...(object as ChromeMessage),
207207
},
208208
];
209209
}
210-
return Object.entries(object).flatMap(([key, value]) =>
211-
_parseMessagesObject(path.concat(key), value, depth + 1),
210+
return Object.entries(object as Record<string, unknown>).flatMap(
211+
([key, value]) =>
212+
_parseMessagesObject(path.concat(key), value, depth + 1),
212213
);
213214
default:
214215
throw Error(`"Could not parse object of type "${typeof object}"`);
215216
}
216217
}
217218

218-
function isPluralMessage(object: any): object is Record<number | 'n', string> {
219+
function isPluralMessage(
220+
object: object,
221+
): object is Record<number | 'n', string> {
219222
return Object.keys(object).every(
220223
(key) => key === 'n' || isFinite(Number(key)),
221224
);
222225
}
223226

224-
function isChromeMessage(object: any): object is ChromeMessage {
227+
function isChromeMessage(object: object): object is ChromeMessage {
225228
return Object.keys(object).every((key) =>
226229
ALLOWED_CHROME_MESSAGE_KEYS.has(key),
227230
);

packages/i18n/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
import { browser } from '@wxt-dev/browser';
1111

1212
export function createI18n<
13-
T extends I18nStructure = DefaultI18nStructure,
13+
T extends I18nStructure | DefaultI18nStructure = DefaultI18nStructure,
1414
>(): I18n<T> {
15-
const t = (key: string, ...args: any[]) => {
15+
const t = (key: string, ...args: unknown[]) => {
1616
// Resolve args
1717
let sub: Substitution[] | undefined;
1818
let count: number | undefined;

packages/i18n/src/types.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ export type I18nStructure = {
77
[K: string]: I18nFeatures;
88
};
99

10-
export type DefaultI18nStructure = {
11-
[K: string]: any;
12-
};
10+
export type DefaultI18nStructure = Record<string, unknown>;
1311

1412
// prettier-ignore
1513
export type SubstitutionTuple<T extends SubstitutionCount> =
16-
T extends 1 ? [$1: Substitution]
14+
T extends 0 ? []
15+
: T extends 1 ? [$1: Substitution]
1716
: T extends 2 ? [$1: Substitution, $2: Substitution]
1817
: T extends 3 ? [$1: Substitution, $2: Substitution, $3: Substitution]
1918
: T extends 4 ? [$1: Substitution, $2: Substitution, $3: Substitution, $4: Substitution]
@@ -22,7 +21,7 @@ export type SubstitutionTuple<T extends SubstitutionCount> =
2221
: T extends 7 ? [$1: Substitution, $2: Substitution, $3: Substitution, $4: Substitution, $5: Substitution, $6: Substitution, $7: Substitution]
2322
: T extends 8 ? [$1: Substitution, $2: Substitution, $3: Substitution, $4: Substitution, $5: Substitution, $6: Substitution, $7: Substitution, $8: Substitution]
2423
: T extends 9 ? [$1: Substitution, $2: Substitution, $3: Substitution, $4: Substitution, $5: Substitution, $6: Substitution, $7: Substitution, $8: Substitution, $9: Substitution]
25-
: never
24+
: []
2625

2726
export type TFunction<T extends I18nStructure> = {
2827
// Non-plural, no substitutions
@@ -37,7 +36,7 @@ export type TFunction<T extends I18nStructure> = {
3736
key: K & { [P in keyof T]: T[P] extends { plural: false; substitutions: SubstitutionCount } ? P : never; }[keyof T],
3837
substitutions: T[K] extends I18nFeatures
3938
? SubstitutionTuple<T[K]['substitutions']>
40-
: never,
39+
: [],
4140
): string;
4241

4342
// Plural with 1 substitution
@@ -62,12 +61,16 @@ export type TFunction<T extends I18nStructure> = {
6261
n: number,
6362
substitutions: T[K] extends I18nFeatures
6463
? SubstitutionTuple<T[K]['substitutions']>
65-
: never,
64+
: [],
6665
): string;
6766
};
6867

69-
export interface I18n<T extends DefaultI18nStructure> {
70-
t: TFunction<T>;
68+
export interface I18n<
69+
T extends I18nStructure | DefaultI18nStructure = DefaultI18nStructure,
70+
> {
71+
t: T extends DefaultI18nStructure
72+
? (key: string, ...args: unknown[]) => string
73+
: TFunction<Extract<T, I18nStructure>>;
7174
}
7275

7376
export type Substitution = string | number;

0 commit comments

Comments
 (0)