Skip to content

Exclude<string, "foo"> is not working #38254

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

Closed
frodo821 opened this issue Apr 29, 2020 · 4 comments
Closed

Exclude<string, "foo"> is not working #38254

frodo821 opened this issue Apr 29, 2020 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@frodo821
Copy link

frodo821 commented Apr 29, 2020

TypeScript Version: 3.8.3 or earlier

Search Terms: Exclude, Conditional Types

Code

type Foo = Exclude<string, "bar">;

// this should cause an error.
const baz: Foo = "bar";

Expected behavior:
"bar" is not assignable to baz.

Actual behavior:
"bar" is assignable to baz without any errors.

Playground Link: https://www.typescriptlang.org/play/#code/FAFwngDgpgBAYgewTAvDAogDwMYBsCuAJlADwDOIATgJYB2A5gDQwBEARgIaUsB8A3MGDYEtCjE4AvAFzwkqVp24ChIsglxQAdLgT0AFJICUAoA

@IllusionMH
Copy link
Contributor

This is limitation of Exclude. Basically it works when you "filter" unions, but string is not a union.

What you are lookin is Negated types: #29317 and comment about your use case #29317 (comment)

@frodo821
Copy link
Author

Thank you for comments!
I guess it means that, in the type system of typescript, string type is not a 'representative' or 'set' of any valid strings.
I thought string means the set of any of it including 'foo', 'bar' or any others of string.

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 30, 2020
@DanielRosenwasser
Copy link
Member

Yeah, unfortunately we're not wired up to "cut holes" out of infinitely large domains of types, and it appears that adding that assumption to the type system would make it incredibly painful to use.

Regarding

https://www.typescriptlang.org/play/?ssl=4&ssc=1&pln=5&pc=1#code/C4TwDgpgBAQghgOwRAJlAvFARAIzgJywG4AoUSKAMQHtqMoBRADwGMAbAVxQgB4BnYPgCWCAOYAaWImQoAfKRItqCAVDwAvKAC4qtergLEoAemNU4Qtn110h1uFAHCxJEgDMOCFsCHKo1AGseABUoCCZgCAQUaycRUVkACgA3HVCAMihE0PDI6Ot4JFQoAH5sGABBADkqhgARLB0AbwBfAEo2qCaSKCh8CGAOfAQoZNIW1yUVYDU4dQB9Nws2ekDEg0JO03NLPkVlVQ15wNWA9YRqebxNkzMAYQALCBYAviA

you might be able to get away with using never in place of BANNED

fangbinwei added a commit to fangbinwei/vue-cli that referenced this issue Jul 14, 2020
since Function belongs to object, so a Function which return type is not object is still assignable
to 'object | () => return type'.  we need exclude Function from object. releated:
microsoft/TypeScript#38254
https://stackoverflow.com/questions/24613955/is-there-a-type-in-typescript-for-anything-except-functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants