Skip to content

Commit c8acd7a

Browse files
authored
Merge pull request #870 from zyf722/i18next-integration-draft
fix(i18n): optimize i18n types performance
2 parents f46a5c6 + a6f5a7a commit c8acd7a

6 files changed

Lines changed: 22 additions & 57 deletions

File tree

.github/workflows/CI-type-checking.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
"i18n-update-push": "node ./scripts/i18n-upload.mjs",
6969
"i18n-update-pull": "node ./scripts/i18n-import.mjs",
7070
"i18n-exclude": "node ./scripts/i18n-exclude.js",
71-
"i18n-lokalise-json": "node ./scripts/i18n-lokalise-json.mjs",
72-
"check-types": "tsc -p tsconfig.full.json --noEmit"
71+
"i18n-lokalise-json": "node ./scripts/i18n-lokalise-json.mjs"
7372
},
7473
"dependencies": {
7574
"@eslint/compat": "1.2.6",

src/livecodes/i18n/locales/models.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { RequireAtLeastOne, UnAsConst } from '../models';
1+
import type { RequireAtLeastOne } from '../models';
22
import type LangInfoTranslation from './en/language-info';
33
import type Translation from './en/translation';
44

55
/**
66
* Add new translatable attributes here.
77
*
8-
* To add new custom data attributes for HTML intellisense, see `script/vscode-intellisense.js`.
8+
* To add new custom data attributes for HTML intellisense, see `scripts/vscode-intellisense.js`.
99
*/
1010
type I18nAttributes = RequireAtLeastOne<{
1111
textContent?: string;
@@ -21,17 +21,24 @@ type I18nAttributes = RequireAtLeastOne<{
2121
* Only use in `en` language with `as const satisfies`.
2222
*/
2323
export interface I18nTranslationTemplate {
24-
[key: string]: ValidI18nTypes | string;
24+
[key: string]: I18nAttributes | string | I18nTranslationTemplate;
2525
}
2626

27-
type ValidI18nTypes = I18nAttributes | I18nTranslationTemplate;
27+
/**
28+
* Maps a nested object structure to a structure where all leaf nodes are strings.
29+
*
30+
* Use to keep the same structure as the `en` i18n object for other languages.
31+
*/
32+
export type I18nStructure<T> = {
33+
readonly [K in keyof T]: T[K] extends Record<string, unknown> ? I18nStructure<T[K]> : string;
34+
};
2835

2936
/**
3037
* Type for all i18n object of namespace `translation` other than `en`.
3138
*/
32-
export type I18nTranslation = UnAsConst<typeof Translation, ValidI18nTypes, string>;
39+
export type I18nTranslation = I18nStructure<typeof Translation>;
3340

3441
/**
3542
* Type for all i18n object of namespace `language-info` other than `en`.
3643
*/
37-
export type I18nLangInfoTranslation = UnAsConst<typeof LangInfoTranslation, ValidI18nTypes, string>;
44+
export type I18nLangInfoTranslation = I18nStructure<typeof LangInfoTranslation>;

src/livecodes/i18n/models.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@ import type { CustomTypeOptions } from 'i18next';
33
/**
44
* Report error when no property is provided.
55
*/
6-
export type RequireAtLeastOne<T> = {
7-
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
8-
}[keyof T];
9-
10-
/**
11-
* Widen `T` to `U` while keeping the structure, with non-object properties widened to `Default`.
12-
*
13-
* @param T The object to be converted.
14-
* @param U The type to be converted to.
15-
* @param Default The default type.
16-
*/
17-
export type UnAsConst<T, U, Default> = RequireAtLeastOne<{
18-
readonly [K in keyof T]: T[K] extends U
19-
? RequireAtLeastOne<UnAsConst<T[K], U, Default>>
20-
: Default;
21-
}>;
6+
export type RequireAtLeastOne<T> = T extends any
7+
? keyof T extends never
8+
? never
9+
: {
10+
[K in keyof T]-?: T & Required<Pick<T, K>>;
11+
}[keyof T]
12+
: never;
2213

2314
/**
2415
* Increment a number type by 1.

tsconfig.full.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@
4949
},
5050
"compileOnSave": true,
5151
"include": ["src/**/*.ts"],
52-
"exclude": ["**/node_modules/**", "src/livecodes/i18n/locales/**"]
52+
"exclude": ["**/node_modules/**"]
5353
}

0 commit comments

Comments
 (0)