editor.js/src/types-internal/i18n-internal-namespace.d.ts
Peter Savchenko 63eeec0f3b
Release / 2.18 (#1181)
Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com>
Co-authored-by: Georgy Berezhnoy <gohabereg@gmail.com>
Co-authored-by: tasuku-s <tasuku@freemind.co.jp>
Co-authored-by: Athul Anil Kumar <athul7744@outlook.com>
Co-authored-by: Taly <vitalik7tv@yandex.ru>
Co-authored-by: flaming-cl <51183663+flaming-cl@users.noreply.github.com>
Co-authored-by: Nguyen Ngoc Son <sonnn.se@gmail.com>
Co-authored-by: Sisir Das K <37764463+sis-dk@users.noreply.github.com>
Co-authored-by: Sisir <sisir@hellosivi.com>
2020-06-03 11:17:29 +03:00

69 lines
1.8 KiB
TypeScript

/**
* Decorator above the type object
*/
type Indexed<T> = { [key: string]: T };
/**
* Type for I18n dictionary values that can be strings or dictionary sub-sections
*
* Can be used as:
* LeavesDictKeys<typeof myDictionary>
*
* where myDictionary is a JSON with messages
*/
export type LeavesDictKeys<D> = D extends string
/**
* If generic type is string, just return it
*/
? D
/**
* If generic type is object that has only one level and contains only strings, return it's keys union
*
* { key: "string", anotherKey: "string" } => "key" | "anotherKey"
*
*/
: D extends Indexed<string>
? keyof D
/**
* If generic type is object, but not the one described above,
* use LeavesDictKey on it's values recursively and union the results
*
* { "rootKey": { "subKey": "string" }, "anotherRootKey": { "anotherSubKey": "string" } } => "subKey" | "anotherSubKey"
*
*/
: D extends Indexed<any>
? { [K in keyof D]: LeavesDictKeys<D[K]> }[keyof D]
/**
* In other cases, return never type
*/
: never;
/**
* Provide type-safe access to the available namespaces of the dictionary
*
* Can be uses as:
* DictNamespaces<typeof myDictionary>
*
* where myDictionary is a JSON with messages
*/
export type DictNamespaces<D extends object> = {
/**
* Iterate through generic type keys
*
* If value under current key is object that has only one level and contains only strings, return string type
*/
[K in keyof D]: D[K] extends Indexed<string>
? string
/**
* If value under current key is object with depth more than one, apply DictNamespaces recursively
*/
: D[K] extends Indexed<any>
? DictNamespaces<D[K]>
/**
* In other cases, return never type
*/
: never;
}