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 click

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2025-01-20 19:00:18

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.

As the Predicate Generator is all about trust, type safety and correctness, why not to go the extra mile and also generate a test suite for the resulting type predicate function right next to the predicate itself!

Ok, we're generating tests for a type predicate function. Where to start? Let's first take a look at a simple predicate function.

You can see that this function uses a typeof operator to check if value is of the primitive type number. In JS/TS all numbers are of this type. Then the function uses the satisfies type operator to verify that the type of value is properly narrowed down to number which satisfies the desired type MyNumber.

That would be great to end the story here, but in TypeScript the body of a type predicate function is not checked to be actually correct in regards of examining the given value. The only requirement is that it returns a boolean. One can easily write a function like below and get no errors from the TypeScript compiler:

Leave a Comment