export type Join = KP extends [infer A] ? A extends string ? A : never : KP extends [infer A, ...infer B] ? A extends string ? `${A}.${Join}` : never : never; export type Split = KP extends `${infer A}.${infer B}` ? [A, ...Split] : [KP]; export type FlagScalar = string | number | boolean; export type Flags = { [key: string]: FlagScalar | Flags }; export type KeyPath = { [Key in keyof T & string]: T[Key] extends object ? [Key, ...KeyPath] : [Key]; }[keyof T & string]; export type KeyPathString = Join>; export type Subscriber = () => void; export type Unsubscribe = () => void; export type Notifier = () => void; export type ExternalStore = { getSnapshot(): T; getServerSnapshot(): T; subscribe(sub: Subscriber): Unsubscribe; }; export type GetValueFromKeyPath> = KP extends [infer K, ...infer Rest] ? /** * `GetValueFromKeyPath` produces errors because we haven't proven that `K` is a key of `T` * or that `Rest` is a `KeyPaths`. As we just derived the key path from `T`, I'm pretty sure * we can safely assume that these constraints are met. */ // @ts-ignore GetValueFromKeyPath : T extends FlagScalar ? T : never; export type GetValueFromKeyPathString>> = Split extends KeyPath ? GetValueFromKeyPath> : never;