Skip to content

Why do TypeScript function and index signatures require naming variables? #13152

Closed
@pelotom

Description

@pelotom

TypeScript Version: all versions up to 2.1.4

This is something that's confused me for a long time about TypeScript. Why is it required to name the parameter x in the type expression (x: A) => B? I know it's possible to use typeof with an expression involving the term, but this is a fairly niche use case, so that most of the time the variable goes unused. I understand that for documentation purposes it can be nice to name the function variables in a type signature. I'm fine with that, but far more important to me is knowing the type of the argument. When first learning TypeScript I was very surprised to learn that, while (A) => B is a valid type expression, it takes the A to be the name of the argument, not the type, and assumes you meant the type to be any! Turning on noImplicitAny eliminates the bugs that can result from that confusion, but it also renders the shorthand (A) => B unusable.

Is it too late to reconsider this design decision? Since A => B (without the parens around A) is currently syntactically invalid as a type, it should be completely backwards compatible to support it as a shorthand for (__nameless_variable__: A) => B.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions