microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.16k stars 12.38k forks source link

Feature Request: expose getPromisedTypeOfPromise #30774

Open maclockard opened 5 years ago

maclockard commented 5 years ago

Search Terms

Suggestion

expose getPromisedTypeOfPromise in the TypeChecker API.

Use Cases

Writing lint rules with typechecking involving promises. One example is one checking the result of a yield in a generator. Since yields inside generators are not typed, being able to use linting to check types are properly annotated can be powerful.

Examples

// source code
const myAsyncFunc = async (num: number) => num;

function* gen() {
   const y = yield call(myFunc);
}

call is a redux saga effect that has an API contract of calling a passed function and awaiting if it is a promise. Given this contract, its possible to write a lint rule instructing a consumer to annotate y with : number.

// lint rule
...
let returnType = appliedFunctionType.getCallSignatures()[0].getReturnType();

if(// is promise) {
  returnType = typeChecker.getPromisedTypeOfPromise(returnType);
}

Checklist

My suggestion meets these guidelines:

RyanCavanaugh commented 5 years ago

@rbuckton thoughts?

d2201 commented 3 years ago

any updates on this feature request?