ts-any-to-type README
Automatically infer and replace any in TypeScript code with specific types.
Features
- Automatically replace
any with inferred types in TypeScript.
- Supports nested objects, arrays, and functions.
- Easy integration with VS Code Lightbulb suggestions.
- Test your code changes with embedded unit testing.
- Extensible with class-based inference for new scenarios.
Installation
- Open VS Code.
- Go to the Extensions Marketplace (Ctrl+Shift+X).
- Search for
TS-Any-to-Type .
- Click
Install .
Usage
- Hover over a TypeScript
any type.
- Click the lightbulb suggestion or run the command
Convert Any to Specific Type .
- View the inferred type and confirm to replace it in your code.
Example
Before:
let obj: any = { name: "John", age: 30 };
Before:
let obj: { name: string; age: number } = { name: "John", age: 30 };
Test Cases
Code |
Expected Type |
Result |
let x: any = "text"; |
string |
✅ |
let y: any = [1, 2, 3]; |
number[] |
✅ |
let z: any = { id: 123 }; |
{ id: number } |
✅ |
let a: any = Math.random() > 0.5 ? "A" : 42; |
string \| number |
✅ |
onChange(value: any) {} |
{ property: unknown } |
✅ |
Supported Scenarios
- Simple variable declarations:
let x: any = "text";
- Nested object structures:
Copy code
let obj: any = { name: "Alice", age: 25 };
Functions:
Copy code
let func: any = (a: number, b: number) => a + b;
Roadmap
- Support for complex conditional types.
- Customizable inference rules.
- Improved performance for large files.
Known Issues
- Cannot infer types from dynamic object properties.
- Limited support for union and intersection types in deeply nested structures.
Contributing
We welcome contributions! Please fork the repository and submit a pull request with your changes.
Feedback
Please report any issues here.
| |