microsoft / microsoft/TypeScript

Suggestion: shorthand syntax for annotating function with type

Open
#16,334 1 comment 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

I like to define types for my functions by separating the type out from the value:

type id = <A>(a: A) => A;
const id: id = a => a;

This helps particularly for longer functions where the parameter list often spans many columns and multiple lines.

However, compared to Elm/Haskell, this code is quite boilerplate-y. I wonder if there is some shorthand syntax we could add to make this pattern more concise. For example, the same code in Elm/Haskell:

id : a -> a
id a = a

The boilerplate in the current TypeScript syntax is:

  • Manual annotation of value (id) with type of same name (id). Could this be automatic, e.g. matched by name?
  • TypeScript requires parameter names for the function type, but are these always required? Could the function type simply by A => A, omitting the parameter names? Or would this cause problems?

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

Start by comparing the proposed TypeScript shorthand with the Elm and Haskell examples in the issue. Define the accepted syntax and its type-checking behavior before identifying implementation and test locations; the issue does not specify files, entry points, or a concrete completion criterion.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.