Skip to content

Add flag to disable Type Inference for variables, Force to declare type #28186

@frank-orellana

Description

@frank-orellana

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

  1. 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.

  1. 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:

  • 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)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions