/** * Decorator above the type object */ type Indexed = { [key: string]: T }; /** * Type for I18n dictionary values that can be strings or dictionary sub-sections * * Can be used as: * LeavesDictKeys * * where myDictionary is a JSON with messages */ export type LeavesDictKeys = 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 ? 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 ? { [K in keyof D]: LeavesDictKeys }[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 * * where myDictionary is a JSON with messages */ export type DictNamespaces = { /** * 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 /** * If value under current key is object with depth more than one, apply DictNamespaces recursively */ : D[K] extends Indexed ? DictNamespaces /** * In other cases, return never type */ : never; }