Type any
overriding all other type definitions in a union type
#18568
Labels
Working as Intended
The behavior described is the intended behavior; this is not a bug
TypeScript Version: 2.5.2
Code
Expected behavior:
testOne
will report that it allows typesstring | number | { foo: string } | any
.testTwo
will report that it allows types'foo' | 'bar' | string
Actual behavior:
testOne
reports that it allows typeany
.testTwo
reports that it allows typestring
.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
andstring
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.
The text was updated successfully, but these errors were encountered: