mirror of
https://github.com/muerwre/muerwre.github.io.git
synced 2025-04-25 02:46:39 +07:00
added whole content
This commit is contained in:
parent
1b5df685cb
commit
8b25e0631a
70 changed files with 5962 additions and 19 deletions
38
content/Typescript/Flatten object with periods.md
Normal file
38
content/Typescript/Flatten object with periods.md
Normal file
|
@ -0,0 +1,38 @@
|
|||
This helper generates Typescript types for i18n dictionary json
|
||||
files by flattening it with period delimiter. Supports plural forms.
|
||||
|
||||
Used for typing [i18n.js](https://www.npmjs.com/package/i18n-js) dictionaries;
|
||||
|
||||
```typescript
|
||||
import en from './en.json';
|
||||
type TranslationPath = Flatten<typeof en>;
|
||||
|
||||
const t = (key: TranslationPath, options?: TranslateOptions) =>
|
||||
I18nLib.t(key, options);
|
||||
```
|
||||
|
||||
Flatten type defined here:
|
||||
|
||||
```typescript
|
||||
// This one based on answer from StackOverflow:
|
||||
// https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object
|
||||
|
||||
export type Flatten<T, D extends number = 5> = [D] extends [never]
|
||||
? never
|
||||
: T extends PluralForm // plural object
|
||||
? ''
|
||||
: T extends object
|
||||
? { [K in keyof T]-?: Join<K, Flatten<T[K], Prev[D]>> }[keyof T]
|
||||
: '';
|
||||
|
||||
// Fix it for you plural form
|
||||
type PluralForm = Record<'one' | 'few' | 'many', string>;
|
||||
|
||||
type Join<K, P> = K extends string | number
|
||||
? P extends string | number
|
||||
? `${K}${'' extends P ? '' : '.'}${P}`
|
||||
: never
|
||||
: never;
|
||||
|
||||
type Prev = [never, 0, 1, 2, 3, 4, 5, ...Array<0>];
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue