Suggestion
I would like to have a flag that would allow me to disable inference of types when declaring variables and assigning the value of a function:
//An error should be shown in this line:
const x = myFunction(); // inferring the type of x from myFunction can cause a lot of problems!!
//And should be corrected to something like this:
const x : string = myFunction(); // type not inferred, we can check now if the function actually returns a string
Use Cases
When I have a variable declaration and assign the value returned by a function, it infers the type of whatever the function returns:
const x = myFunction(); //Maybe myFunction() returns a string, a number, an array, whatever
If I change the return type to a Promise or to a compatible type, it will not show any warning or error, unless I use methods or properties of x that are not on the new type. This may lead to unwanted results.
Examples
- For example, when I have to migrate a function that returned a direct value (string, number, object, etc), to a function that returns a promise:
function myFunction() : Promise<string> { //or async myFunction()...
....
return some_promise; //Maybe before this returned a string
}
const x = myFunction(); //x was a direct value but Now it is a Promise!!, no warning!
if(x != undefined){ //x will never be undefined now because it is a promise!!
do_something_important;
}
So, in this case, I forgot to change the code that uses the return value and no warning is shown. That will lead to unwanted results.
- Another example: fiddler
If I had previously:
function myFunction() : string { return "some string"}
and then change it to:
function myFunction() : string[] { return ["some string"]}
If I was using this as:
const x = myFunction();
console.log(x.length);
then it will again, not show any warning, but the results will be unwanted.
In these cases, if the compiler would have forced me to declare the variable like:
const x : string = myFunction();
instead of inferring the type of x, then I would have gotten the correct warnings.
I created the question for this after a some research in stackoverflow and a fiddler test case of the second point.
The noImplicitAny is useless in this case unless you do not initialize variables when declaring them, but this would prevent the use of const. I'm having a lot of these issues specially when migrating code to promises.
Thank you very much!!
Checklist
My suggestion meets these guidelines:
It meets all, the suggestion is to add a flag, that would be disabled by default anyway, and would only check code at compile time.
Suggestion
I would like to have a flag that would allow me to disable inference of types when declaring variables and assigning the value of a function:
Use Cases
When I have a variable declaration and assign the value returned by a function, it infers the type of whatever the function returns:
If I change the return type to a Promise or to a compatible type, it will not show any warning or error, unless I use methods or properties of
xthat are not on the new type. This may lead to unwanted results.Examples
So, in this case, I forgot to change the code that uses the return value and no warning is shown. That will lead to unwanted results.
If I had previously:
and then change it to:
If I was using this as:
then it will again, not show any warning, but the results will be unwanted.
In these cases, if the compiler would have forced me to declare the variable like:
instead of inferring the type of x, then I would have gotten the correct warnings.
I created the question for this after a some research in stackoverflow and a fiddler test case of the second point.
The
noImplicitAnyis useless in this case unless you do not initialize variables when declaring them, but this would prevent the use ofconst. I'm having a lot of these issues specially when migrating code to promises.Thank you very much!!
Checklist
My suggestion meets these guidelines:
It meets all, the suggestion is to add a flag, that would be disabled by default anyway, and would only check code at compile time.