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

Type any overriding all other type definitions in a union type #18568

Closed
bradzacher opened this issue Sep 19, 2017 · 4 comments
Closed

Type any overriding all other type definitions in a union type #18568

bradzacher opened this issue Sep 19, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@bradzacher
Copy link
Contributor

TypeScript Version: 2.5.2

Code

function testOne(arg : string | number | { foo: string } | any) { }
function testTwo(arg : 'foo' | 'bar' | string) { }

Expected behavior:
testOne will report that it allows types string | number | { foo: string } | any.
testTwo will report that it allows types 'foo' | 'bar' | string

Actual behavior:
testOne reports that it allows type any.
testTwo reports that it allows type string.


Sometimes you want to allow a variable to be a given type, a given structure, whilst also providing a general fallback any typing.
The problem is that typescript seems to collapse the types together into the most generic types possible.

Because of how typescript reports the variable types, editors like VSCode report the function arguments as any and string respectively, thus preventing any granular definitions/autocompletes.

I believe this could be resolved by something like an exclusive or operator (such as #14094), as long as the logic is coded such that any is always the last type to be checked.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 19, 2017

a union with any will always be any. we relay on this in the implementation to optimize union type comparisons.
for cases like this we recommend you use overloads.

function testTwo(arg : 'foo' | 'bar');
function testTwo(string);

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 19, 2017
@bradzacher
Copy link
Contributor Author

That would work for functions, but what about for things like object properties?

The reason thar I've run into this is because of the way react types its styles attribute. There are a lot of scenarios where the CSS spec allows exact keywords, as well as any value. Because typescript collapses the type to any, you lose autocomplete on the exact string values.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 19, 2017

The reason thar I've run into this is because of the way react types its styles attribute. There are a lot of scenarios where the CSS spec allows exact keywords, as well as any value. Because typescript collapses the type to any, you lose autocomplete on the exact string values.

I understand. but as i said we rely on some of these optimizations to be able to do comparisons between union types in a performant fashion.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 4, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants