microsoft / microsoft/TypeScript
Differentiate between implicit any and explicit any in the Compiler API.
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
- "Differentiate between implicit any and explicit any in the Compiler API."
Suggestion
Working with the compiler API I've noticed the type checker doesn't differentiate between any originating from a decision by the author and a default case by the compiler.
In an internal branch I've fixed this by adding a new field to ts.TypeFlags and a new intrinsic type which is any with the flag added. I'm happy to upstream my implementation if this is accepted.
Use Cases
function hello(a, b) {}
function world(a: any, b: any) {
const c = b;
}
In the current version of TypeScript these 2 methods take identical arguments and can't be differentiated by the signatures alone. Adding explicit any to the compiler allows tools to distinguish these.
I've found this modification particularly valuable when utilizing the type checker for tools that analyse the output of the compiler. An example I've been using this for internaly is calculating the explicit type coverage of TypeScript code which could otherwise not be done.
Examples
if (ts.isIdentifier(node)) {
const type = this.checker.getTypeAtLocation(node);
if (type.getFlags() & ts.TypeFlags.Any &&
!(type.getFlags() & ts.TypeFlags.Explicit)) {
this._untypedIdentifiers += 1;
} else {
this._typedIdentifiers += 1;
}
}
With the compiler change it's now possible to exclude explicit any from type coverage calculations.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. new expression-level syntax)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the Compiler API's type checker, ts.TypeFlags, and getTypeAtLocation, then compare the issue's implicit-any and explicit-any examples. Done means the API can distinguish author-written any from compiler-default any without changing emitted JavaScript, with the proposed flag and intrinsic type behavior documented and tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100