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

Proposal: is.create(URLSearchParams) #135

Open
szmarczak opened this issue Jul 30, 2021 · 3 comments
Open

Proposal: is.create(URLSearchParams) #135

szmarczak opened this issue Jul 30, 2021 · 3 comments

Comments

@szmarczak
Copy link

const isSearchParams = is.create(URLSearchParams);

is.any([is.string, isSearchParams]);
@szmarczak szmarczak changed the title Proposal: is.create(URL) Proposal: is.create(URLSearchParams) Jul 30, 2021
@sindresorhus
Copy link
Owner

Can you describe how exactly it should behave? Because you're passing in the constructor, which would mean you want a instanceof check internally, but that's not a good or portable way to check.

@szmarczak
Copy link
Author

that's not a good or portable way to check.

Is there a better choice? We could compare the names but that's not good either - e.g. there are many FormData implementations but they differ.

The create function returns another function. But it can have an extra feature: if used with any it will display that class name: E.g. Expected string or URLSearchParams instance, got ...

@sindresorhus
Copy link
Owner

sindresorhus commented Jul 31, 2021

Pass in a string and use

const isObjectOfType = <T>(type: ObjectTypeName) => (value: unknown): value is T => getObjectType(value) === type;

That's how most of the testing is done here.


Yes, we could read the name from the passed in constructor. I just fear that it will make people assume it does instance checking, which it does not.

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

No branches or pull requests

2 participants