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

Validate unidirectional assignability #39

Closed
SamVerschueren opened this issue Sep 27, 2019 · 8 comments
Closed

Validate unidirectional assignability #39

SamVerschueren opened this issue Sep 27, 2019 · 8 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@SamVerschueren
Copy link
Collaborator

Since 0.9.0, expectType assertions are strict. Buy maybe this isn't always what we want.

type Color = 'red' | 'blue';

expectType<Color>('red');

This will fail because Color is not assignable to red unless you explicitly cast it to Color.

type Color = 'red' | 'blue';

expectType<Color>('red' as Color);

Should we add a loose (or unidirectional) expectType variant for these cases?

@SamVerschueren SamVerschueren added help wanted Extra attention is needed question Further information is requested labels Sep 27, 2019
@SamVerschueren
Copy link
Collaborator Author

We could either expose a new assertion type, which has the same behaviour as the old expectType assertion. Or we can add a config flag or object as second parameter.

expectTypeLoose<Color>('red');

expectType<Color>('red', false);

Second parameter is a boolean indicating if the assertion is strict or not.

I think I'm slightly more in favour of a separate assertion as the intention is much clearer.

@danvk
Copy link

danvk commented Oct 8, 2019

Instead of expectTypeLoose, how about expectAssignable? Since I think that's really what you're testing.

@SamVerschueren
Copy link
Collaborator Author

I like that...

@sindresorhus
Copy link
Collaborator

I like expectAssignable. Should there also be a expectNotAssignable?

@SamVerschueren
Copy link
Collaborator Author

I believe a not counter part for maybe all assertions might be a good idea. For example, expectNotType might be useful as well.

@sindresorhus
Copy link
Collaborator

Agreed

@SamVerschueren
Copy link
Collaborator Author

I created a new issue (see #54) which I need to create the expectNotAssignable assertion. So for now, I will only implement expectAssignable until #54 is fixed.

@SamVerschueren
Copy link
Collaborator Author

When I was in the shower this morning, I figured out how I could easily add the expectNotAssignable assertion. So here it is, added it as well 🎉 :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants