Skip to content

Instantly share code, notes, and snippets.

@Tomas2D
Last active January 11, 2022 11:40
Show Gist options
  • Select an option

  • Save Tomas2D/939540879842d15087db07ffbef3f84c to your computer and use it in GitHub Desktop.

Select an option

Save Tomas2D/939540879842d15087db07ffbef3f84c to your computer and use it in GitHub Desktop.

Revisions

  1. Tomas2D revised this gist Jan 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion i18n.types.ts
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ type PathsWithStringValue<T, D extends number = 10> = [D] extends [never]
    : T extends string
    ? []
    : {
    [K in Extract<keyof T, string>]: [K, ...PathWithStringValue<T[K], Prev[D]>];
    [K in Extract<keyof T, string>]: [K, ...PathsWithStringValue<T[K], Prev[D]>];
    }[Extract<keyof T, string>];

    type Join<T extends string[], D extends string> = T extends []
  2. Tomas2D revised this gist Jan 10, 2022. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions i18n.types.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,15 @@
    import * as error from './cs/error.json';
    /*
    // Content of a "./cs/error.json" file
    {
    "general": {
    "unexpected": "Došlo k neočekávané chybě."
    },
    "unauthorized": {
    "token": "Váš přístup byl zamítnut, nevalidní token."
    }
    }
    */

    type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]];

    @@ -23,3 +34,9 @@ type Join<T extends string[], D extends string> = T extends []
    type DottedStringPaths<T> = Join<PathsWithStringValue<T>, '.'>;

    export type ErrorTranslationPath = `error.${DottedStringPaths<typeof error>}`;

    let transErrorId: ErrorTranslationPath;
    transErrorId = 'error.general.unexpected' // OK
    transErrorId = 'error.general' // Error, `general` is not a string value
    transErrorId = 'error.unauthorized' // Error, `unauthorized` is not a string value
    transErrorId = 'error.unauthorized.token' // OK
  3. Tomas2D revised this gist Jan 9, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions i18n.types.ts
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import * as error from './cs/error.json';

    type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]];

    type PathWithStringValue<T, D extends number = 10> = [D] extends [never]
    type PathsWithStringValue<T, D extends number = 10> = [D] extends [never]
    ? never
    : T extends string
    ? []
    @@ -20,6 +20,6 @@ type Join<T extends string[], D extends string> = T extends []
    : never
    : string;

    type DottedLanguageObjectStringPaths<T> = Join<PathWithStringValue<T>, '.'>;
    type DottedStringPaths<T> = Join<PathsWithStringValue<T>, '.'>;

    export type ErrorTranslationPath = `error.${DottedLanguageObjectStringPaths<typeof error>}`;
    export type ErrorTranslationPath = `error.${DottedStringPaths<typeof error>}`;
  4. Tomas2D created this gist Jan 9, 2022.
    25 changes: 25 additions & 0 deletions i18n.types.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    import * as error from './cs/error.json';

    type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]];

    type PathWithStringValue<T, D extends number = 10> = [D] extends [never]
    ? never
    : T extends string
    ? []
    : {
    [K in Extract<keyof T, string>]: [K, ...PathWithStringValue<T[K], Prev[D]>];
    }[Extract<keyof T, string>];

    type Join<T extends string[], D extends string> = T extends []
    ? never
    : T extends [infer F]
    ? F
    : T extends [infer F, ...infer R]
    ? F extends string
    ? `${F}${D}${Join<Extract<R, string[]>, D>}`
    : never
    : string;

    type DottedLanguageObjectStringPaths<T> = Join<PathWithStringValue<T>, '.'>;

    export type ErrorTranslationPath = `error.${DottedLanguageObjectStringPaths<typeof error>}`;