diff options
| author | ST-DDT <[email protected]> | 2022-01-23 12:34:16 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-23 12:34:16 +0100 |
| commit | c1f2b09be957760a133baaab9d58364bda57be0d (patch) | |
| tree | 6384519802fea7ee124cb5b8c26333e9b14a4d56 /src/index.ts | |
| parent | 6ba72a42aef3c244ec0dac29955ac49c161c0c1b (diff) | |
| download | faker-c1f2b09be957760a133baaab9d58364bda57be0d.tar.xz faker-c1f2b09be957760a133baaab9d58364bda57be0d.zip | |
feat: add autocomplete support for locales (#248)
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/index.ts b/src/index.ts index f0e98517..a42aa798 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ import { Hacker } from './hacker'; import { Helpers } from './helpers'; import { Image } from './image'; import { Internet } from './internet'; +import type { KnownLocale } from './locales'; import allLocales from './locales'; import { Lorem } from './lorem'; import { Mersenne } from './mersenne'; @@ -25,6 +26,9 @@ import { Unique } from './unique'; import { Vehicle } from './vehicle'; import { Word } from './word'; +// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609 +type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never }); + export interface LocaleDefinition { title: string; separator?: string; @@ -186,12 +190,13 @@ export interface LocaleDefinition { [group: string]: any; } +export type UsableLocale = LiteralUnion<KnownLocale>; +export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>; + export interface FakerOptions { - locales?: { - [locale: string]: LocaleDefinition; - }; - locale?: string; - localeFallback?: string; + locales?: UsedLocales; + locale?: UsableLocale; + localeFallback?: UsableLocale; } export interface DefinitionTypes { @@ -216,11 +221,9 @@ export interface DefinitionTypes { } export class Faker { - locales: { - [locale: string]: LocaleDefinition; - }; - locale: string; - localeFallback: string; + locales: UsedLocales; + locale: UsableLocale; + localeFallback: UsableLocale; // TODO @Shinigami92 2022-01-11: For now we loose types here // @ts-expect-error: will be lazy filled by constructor @@ -432,7 +435,7 @@ export class Faker { * * @param locale */ - setLocale(locale: string): void { + setLocale(locale: UsableLocale): void { this.locale = locale; } } |
