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

Feature Request: Shortcut to simplify const v = func(); is<type>(v) ? v : default #109

Open
oleg-slapdash opened this issue Jul 30, 2021 · 1 comment

Comments

@oleg-slapdash
Copy link
Contributor

I've noticed that I've been used this pattern for a while now. It would make a good quality of life improvement if I could check the type and then default to another value if the type check fails.

A common use case is when I need to call some function and then do verification:

const foo = JSON.parse('null');
const bar = is<boolean>(foo) ? foo : false;

This should be easy to simplify to:

const bar = ifType<boolean>(JSON.parse('null'), false);

The function name (ifType) is up to you

@oleg-slapdash
Copy link
Contributor Author

Also, as an inspiration, we can look at SQL COALESCE function - https://docs.faircom.com/doc/sqlref/33405.htm. In this case, we can extend our typedef to the following: coalsceType<Target>(maybeTarget1, maybeTarget2, maybeTarget3);

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

1 participant