mockdeep / typewiz

Automatically discover and add missing types in your TypeScript code
https://medium.com/@urish/manual-typing-is-no-fun-introducing-typewiz-58e3e8813f4c
1.1k stars 46 forks source link

Find more types using Type Inference #27

Closed urish closed 6 years ago

urish commented 6 years ago

Many times, TypeScript can infer the types of arguments you pass in a function call by looking at their definition. Consider the following example:

const promise = Promise.resolve(15);
f(promise);

In this case, TypeScript can infer the type of promise as Promise<number>. We can take advantage of this information to add type information to the first parameter of f. So given the following code as input:

function f(a) {
  return a;
}

const promise = Promise.resolve(15);
f(promise);

We should be able to discover the type of argument a correctly, and get the following result:

function f(a: Promise<number>) {
  return a;
}

const promise = Promise.resolve(15);
f(promise);

This PR adds experimental support for this feature. At this time, the code is still work-in-progress and is not yet ready to be merged.