pbeshai / use-query-params

React Hook for managing state in URL query parameters with easy serialization.
https://pbeshai.github.io/use-query-params
ISC License
2.16k stars 96 forks source link

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

Closed Shaddix closed 2 years ago

Shaddix commented 2 years ago

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 .

Shaddix commented 2 years ago

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)

pbeshai commented 2 years ago

Fixed in #198