feat: add arrays to IndexedStorage typings

Signed-off-by: Wouter Termont <woutermont@gmail.com>
This commit is contained in:
Wouter Termont 2024-03-11 13:20:41 +01:00
parent df9062cfc5
commit ccc006695f
No known key found for this signature in database

View File

@ -8,17 +8,20 @@ export const INDEX_ID_KEY = 'id';
* Valid values are `"string"`, `"boolean"`, `"number"` and `"id:TYPE"`,
* with TYPE being one of the types in the definition.
* In the latter case this means that key points to an identifier of the specified type.
* A `[]` can be appended to the type to indicate the value is an array.
* A `?` can be appended to the type to indicate this key is optional.
*/
export type ValueTypeDescription<TType = string> =
`${('string' | 'boolean' | 'number' | (TType extends string ? `${typeof INDEX_ID_KEY}:${TType}` : never))}${
'[]' | ''}${
'?' | ''}`;
/**
* Converts a {@link ValueTypeDescription} to the type it should be interpreted as.
*/
export type ValueType<T extends ValueTypeDescription> =
(T extends 'boolean' | 'boolean?' ? boolean : T extends 'number' | 'number?' ? number : string) |
(T extends `${infer E extends ValueTypeDescription}[]${'?' | ''}` ? ValueType<E>[] :
T extends 'boolean' | 'boolean?' ? boolean : T extends 'number' | 'number?' ? number : string) |
(T extends `${string}?` ? undefined : never);
/**