microsoft / microsoft/TypeScript

Feature Request: expose getPromisedTypeOfPromise

Open
#30,774 3 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Apr 10, 2019.

Domain: API Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

  • getPromisedTypeOfPromise
  • detect Promise

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:

  • 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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.