Skip to content

Better implicit function expression #25501

Closed
@KSXGitHub

Description

@KSXGitHub

Search Terms

Suggestion

Given this code:

const foo = x => bar(x)

Currently, it is translated to:

const foo = (x: any) => bar(x)

But I think this would be better:

declare function bar<T> (x: T): Result
const foo = <T0>(x: T0) => bar(x)
declare function bar (x: Param): Result
const foo = (x: Param) => bar(x)

Use Cases

When bar is generic

declare function bar<A, B, C, R> (a: A, b: B, ...rest: C[]): R
const foo = (a, b, c, d) => bar(a, b, c, d)

Should be converted to:

declare function bar<A, B, C, R> (a: A, b: B, ...rest: C[]): R
const foo = <T0, T1, T2>(a: T0, b: T1, c: T2, d: T2) => bar(a, b, c, d)

When bar isn't generic

declare function bar (a: A, b: B, ...rest: Rest[]): Result
const foo = (a, b, c, d) => bar(a, b, c, d)

Should be converted to:

declare function bar (a: A, b: B, ...rest: Rest[]): Result
const foo = (a: A, b: B, c: Rest, d: Rest) => bar(a, b, c, d)

When some parameters are irrelevant

Use any

const foo = (a, b) => b

Should be converted to:

const foo = <T0>(a: any, b: T0) => b

Examples

x => x wouldn't return any

const fn = x => x
const x = 123
const y: number = fn(x)

x => [x] wouldn't return any[]

const fn = x => [x]
const x = 123
const y: number[] = fn(x)

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. new expression-level syntax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions