UNPKG

typesafe-context-hook

Version:

A react-typescript utility for creating typesafe React context hooks. It provides a function that takes a name and a hook, and returns an object with a custom hook, a provider component, and a higher-order component. The custom hook can only be used withi

10 lines (9 loc) 539 B
import { type FC, type ReactNode } from "react"; type ProviderProps<TArgs> = { children?: ReactNode; } & TArgs; type Result<TValue, TName extends string, TArgs> = { [K in `use${TName}Context` | `${TName}Provider`]: K extends `use${TName}Context` ? () => TValue : K extends `${TName}Provider` ? FC<ProviderProps<TArgs>> : never; }; declare function typesafeContextHook<TName extends string, TValue, TArgs extends object>(name: TName, hook: (args: TArgs) => TValue): Result<TValue, TName, TArgs>; export default typesafeContextHook;