microsoft / microsoft/TypeScript

generate function declaration from function type in TypeScript

Open
#22,395 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: LS: Quick Fixes Experience Enhancement In Discussion Suggestion VS Code Tracked
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

From @unional on October 5, 2017 18:52

type MyCallback = (a: number, b: string) => void

function foo(callback: MyCallback) {
  // ...
}

// case 1
foo((a, b) => { ... })

// case 2
foo(function (a, b) { ... })

// case 3
function cb(a: number, b: string) { ... }
foo(cb)

Above are 3 common ways to write a callback.
TypeScript is great at inferring the argument types in the implemented callbacks.
a, and b are typed accordingly in the callbacks above.

However, the tooltip is not useful when we are implementing the callbacks:

image

If I change foo() to not using type alias, it helps in case 1 and 2:

function foo(callback: (a: number, b: string) => void) { ... }

image

But that doesn't help in case 3 and actually demotes type alias.

One solution I can think of is using [ctrl+.] to fill in the callback:

foo(/* caret */)

// [ctrl+.] and choose "implement callback as arrow function":
foo((a: number, b: string) => { /* caret */ })

// [ctrl+.] and choose "implement callback as function declaration":
foo(function (a: number, b: string) { /* caret */ })

// caret anywhere on the type alias below
MyCallback

// [ctrl+.] and choose "implement function declaration" will expand to snippet:
function ${1:functionName}(a: number, b: string): void { ${0} }

Copied from original issue: Microsoft/vscode#35635

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.

Research direction

The issue does not name implementation files or tests. Start by tracing the ctrl+. code-action entry point for callback and function-declaration generation, then verify that the requested actions support inferred callback parameters and type aliases in all three examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.