Given a variable declaration like let i = 0
or const j = getSomething();
, the task is to determine if the variable is an integer, float, or another type (not just numeric vs non-numeric). The approach involves identifying all instances where the value is assigned, determining their types recursively, and checking if any of them are set to something other than an integer.
The available resources include a ts.TypeChecker
and ts.Program
. Daniel Rosenwasser's tweets suggest that the checker API is the way to proceed, but it can be challenging to identify the specific APIs to utilize.
For further details, refer to the relevant GitHub issue and the corresponding source file.
It is not as straightforward as using typeChecker.getSymbolAtLocation
. Consider the example below:
function someScope() {
let myInt = 0;
let myFloat = 0;
for (let i = 0; i < 3; i += 1) {
myInt = i;
myFloat = i;
}
if (external) {
myInt = 7;
} else {
myFloat = 7.5;
}
return myInt * myFloat;
}
The objective is to develop a program that traverses the tree and correctly identifies myInt
as an integer (not just a generic number) while recognizing myFloat
as a float. Additionally, the function someScope
should be understood to return a float (not an integer).
In the "worst-case" scenario, the strategy would involve analyzing the variables' containing scope, finding all references to them, and recursively determining their types. Surely, there must exist a standard method to achieve this?