There are numerous questions as to why any cannot be narrowed from. Despite being natural and intuitive this feature was rejected due to being a serious breaking change to poorly written yet massive code bases.
Please consider putting it behind a flag provided the evident need for it.
TLDR
any is not what you think it is (poor naming at play)
- for narrowing use
type unknown = {} | undefined | null | void instead
- yes, it's a hot unfortunate mess
- no, there is no painless way to fix it
- yes, you gonna need to fix your
*.d.ts by hands
- why? to make average JS developers happy with their less than perfect code
- welcome to the club! get your badge at the reception
UPDATE
@yortus made a branch per #9999 (comment) where narrowing from any works!
try it: npm install typescript-narrowany
There are numerous questions as to why
anycannot be narrowed from. Despite being natural and intuitive this feature was rejected due to being a serious breaking change to poorly written yet massive code bases.Please consider putting it behind a flag provided the evident need for it.
TLDR
anyis not what you think it is (poor naming at play)type unknown = {} | undefined | null | voidinstead*.d.tsby handsUPDATE
@yortus made a branch per #9999 (comment) where narrowing from
anyworks!try it:
npm install typescript-narrowany