allow narrowing from any
Problem
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 https://github.com/Microsoft/TypeScript/issues/9999#issuecomment-238070961 where narrowing from `any` works! try it: `npm install typescript-narrowany`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Flag for Narrowing from Any in TypeScript
The TypeScript compiler does not allow narrowing from the 'any' type due to its design choice to prevent breaking changes in existing codebases. This decision stems from the potential for widespread issues in legacy code that relies on 'any' being a catch-all type without strict type checks. However, the need for narrowing from 'any' has been highlighted as a significant pain point for developers, leading to the proposal of a flag-based solution.
Awaiting Verification
Be the first to verify this fix
- 1
Create a Feature Flag for Narrowing from Any
Introduce a compiler flag (e.g., 'allowNarrowingFromAny') that enables narrowing from 'any' type. This flag should be off by default to maintain backward compatibility.
json// In tsconfig.json { "compilerOptions": { "allowNarrowingFromAny": true } } - 2
Update TypeScript Compiler Logic
Modify the TypeScript compiler's type-checking logic to allow narrowing from 'any' when the 'allowNarrowingFromAny' flag is enabled. Ensure that this change is isolated to avoid affecting existing behavior.
typescript// Pseudocode for compiler logic modification if (compilerOptions.allowNarrowingFromAny) { // Allow narrowing from 'any' } - 3
Document the New Feature
Update the TypeScript documentation to include information about the new flag and how to use it. Provide examples of how narrowing from 'any' works with the flag enabled.
typescript// Example usage in documentation let value: any; if (typeof value === 'string') { // This block is valid when allowNarrowingFromAny is true } - 4
Encourage Codebase Refactoring
Advise developers to refactor their codebases to replace 'any' with more specific types where possible, using 'unknown' or union types instead. This will help improve type safety in the long run.
typescript// Refactoring example let value: unknown; if (typeof value === 'string') { // Handle string case } - 5
Monitor Feedback and Iterate
After releasing the feature, gather feedback from the community regarding its usage and any issues encountered. Use this feedback to make further improvements or adjustments to the feature.
typescript// Example of feedback collection console.log('Please provide feedback on narrowing from any feature');
Validation
To confirm the fix worked, enable the 'allowNarrowingFromAny' flag in your tsconfig.json and test narrowing from 'any' in your TypeScript code. Ensure that the TypeScript compiler allows the narrowing without errors. Additionally, gather feedback from users on their experience with the new feature.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep