pbeshai / use-query-params

React Hook for managing state in URL query parameters with easy serialization.

Home Page:https://pbeshai.github.io/use-query-params

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why createEnumParam doesn't strongly-type the `setParam` function?

Shaddix opened this issue · comments

Here's the definition of createEnumParam:

export const createEnumParam = <T extends string>(
  enumValues: T[]
): QueryParamConfig<string | null | undefined, T | null | undefined>

Is there any reason why return type of it isn't QueryParamConfig<T | null | undefined, T | null | undefined> (the first generic changed from string | null | undefined to T | null | undefined?

For me the latter seems more correct, as it allows strong-typing of setParam function .

My use case:

const MyParam = createEnumParamTyped<'one' | 'two'>([
  'one',
  'two',
]);
const [param, setParam] = useQueryParam('paramName', MyParam);

Here param is properly typed ('one' | 'two' | ...), but setParam isn't (I can pass any string to it)

Fixed in #198