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

Exclude type not preventing to pass "excluded" types #22822

Closed
lazywithclass opened this issue Mar 23, 2018 · 2 comments
Closed

Exclude type not preventing to pass "excluded" types #22822

lazywithclass opened this issue Mar 23, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@lazywithclass
Copy link

lazywithclass commented Mar 23, 2018

TypeScript Version: 2.8.0-dev.20180322

Code

I am experimenting with the Exclude type introduced here #21847.

What I would like is to create a type that includes all strings except for "", I thought something like this would work:

type NonEmptyString = Exclude<string, "">;
function test(s: NonEmptyString) {}
test("");

so I was expecting an error, but no, it compiles fine. Also test("a"); compiles fine. I didn't get the reason why, I've asked on gitter and it looks like this code should give a compile error.

Expected behavior: I think a compiler error should be shown, but I'm really new with TypeScript, so it might easily be an error on my side

Actual behavior: No compiler error, both with test("") and test("a")

@ajafff
Copy link
Contributor

ajafff commented Mar 23, 2018

Exclude only works with union types. string is no union so you would need subtraction types.

Duplicate of #22382

@lazywithclass
Copy link
Author

I see, thanks.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 28, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants