Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic parse function #255

Open
NoiRTvT opened this issue Apr 23, 2020 · 3 comments
Open

Generic parse function #255

NoiRTvT opened this issue Apr 23, 2020 · 3 comments

Comments

@NoiRTvT
Copy link

NoiRTvT commented Apr 23, 2020

So, want to used parse function by generic

export function parse<T = ParsedQuery>(query: string, options?: ParseOptions): T;

And the IDE can suggest the query object's properties.

image

@pvburkov
Copy link

I have also caught common case in practice. It would be really convenient to use this generic with static type checking.

@LinusU
Copy link

LinusU commented May 7, 2020

On DefinitelyTyped there is a lint rule to guard against this, and I would not recommend adding this as it hides an unsafe cast. There is nothing at all that guarantees that active be a boolean, or even exist, at runtime.

If you want to do this in your code, you can always do queryString.parse('...') as Some, which shows that you are casting. Adding a type parameter for this really only hides the as Some part, and makes it easier to miss the unsafety here...

It's the same with JSON.parse, it doesn't take a type parameter.

@jamiehaywood
Copy link

@LinusU could you elaborate on why it would be unsafe? Could you not take the generic passed into the function & wrap a Partial around it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants