react-hooks-global-states
Version:
This is a package to easily handling global-state across your react-components using hooks.
77 lines (76 loc) • 2.35 kB
TypeScript
export declare const __brand: unique symbol;
/**
* Branded unique identifier
*/
export type BrandedId<T extends string | undefined> = `${T extends string ? T : ''}${string}` & {
[__brand]: T;
};
interface UniqueId {
/**
* Generates a unique identifier string, optionally prefixed.
*
* @example
* uniqueId(); // "k9j3n5x8q2"
* type Id1 = `${string}` & { [__brand]: undefined };
*
* uniqueId('user:'); // "user:k9j3n5x8q2"
* type Id2 = `user:${string}` & { [__brand]: 'user:' };
*/
<T extends string | undefined>(prefix?: T): BrandedId<T>;
/**
* Creates a reusable unique ID generator for a specific prefix.
*
* @example
* const makeOrderId = uniqueId.for('order:');
* const id = makeOrderId(); // "order:k9j3n5x8q2"
* type OrderId = `order:${string}` & { [__brand]: 'order:' };
*/
for<T extends string>(prefix: T): {
(): `${T}${string}` & {
[__brand]: T;
};
/**
* Checks if the given value matches the branded ID for this prefix.
*/
is(value: unknown): value is `${T}${string}` & {
[__brand]: T;
};
/**
* Asserts that the value matches this branded ID, throws otherwise.
*/
assert(value: unknown): asserts value is `${T}${string}` & {
[__brand]: T;
};
/**
* Returns a strictly branded generator using a custom symbol brand.
*/
strict<Brand extends symbol>(): {
(): `${T}${string}` & {
[__brand]: Brand;
};
is(value: unknown): value is `${T}${string}` & {
[__brand]: Brand;
};
assert(value: unknown): asserts value is `${T}${string}` & {
[__brand]: Brand;
};
};
};
/**
* Creates a reusable unique ID generator without a prefix.
*/
of<T extends string>(): () => string & {
[__brand]: T;
};
/**
* Creates a strictly branded unique ID generator without a prefix.
*/
strict<Brand extends symbol>(): () => string & {
[__brand]: Brand;
};
}
/**
* Generates a unique identifier string, optionally prefixed.
*/
export declare const uniqueId: UniqueId;
export default uniqueId;