Skip to content

Suggestion: function declarations should have way to reuse established types. #54989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
tbremer opened this issue Jul 12, 2023 · 2 comments
Closed
5 tasks done

Comments

@tbremer
Copy link

tbremer commented Jul 12, 2023

Suggestion

πŸ” Search Terms

  • is:issue function declaration label:Suggestion

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Would love it if I could re-use some defined function type without have to use a function expression. Function declarations should allow for a generic after the keyword function that defines it's arguments and return type.

Example:

type Formatter = (s: string) => `FORMATTED: ${s}.`

// today you have to use a function expression
const fmt:Formatter = (str) => { … }


// ideally something like this is possible
function<Formatter> (str) { … }

Playground link with sample code: https://www.typescriptlang.org/play?#code/PTAEHcHsFcBsBNS0gNwKagIYUwT1AC6SjxoDGsmAThgQBYCWAzqAGbQB2ZBDkHoTBgHMOmAtBoRgxOphQMOQwsWhMM2dlx59QaAB4AHGk0E7IVUDQC04KgwK06GUqwX3eHAHQAoELs9CngBcbJzcHgA8AHJ8UXCUAEawaBEAsrgAkhwOVKyYZGgA2gDk+docTMUAuiUMpNkMrLjVAHwtoHVoDU0AFACUoADeoIBkBKAAvt6a4Tqd3bj9Q6DAAFSjoCtgk36gu3u7AHoA-N7eZHxMBB31PE0ATCExHHGwiclpmdlouflFpTMVaq1G6NZpVdoAXlAiwh7WGq3WmwmvjA+32x1OChyeQKoHSWWxvyG3l2ZQ8TBCgxJezmt1wRxCPTqIUudkUABpdABbTAMWAMgQENlCAaw0AoSB1ADc1N2xjQBAFMPaEulssIaEuStFKsl8Blu0m4yAA

πŸ“ƒ Motivating Example

  1. See playground link above.
  2. This could enable more concise function overloads (example from docs).
type LenString = (s: string) => number;
type LenArr = (arr: any[]) => number;

function<LenString | LenArr> (x) {
    return x.length;
}

πŸ’» Use Cases

  • Allows for type re-use in places where function expressions are unnecessary.
  • Better ergonomics when you have interfaces that define functions.
  • Concise function overloads
@tbremer tbremer changed the title Suggestion: function declarations should have way to re-use established types. Suggestion: function declarations should have way to reuse established types. Jul 12, 2023
@whzx5byb
Copy link

Duplicate of #22063

@tbremer
Copy link
Author

tbremer commented Jul 12, 2023

Duplicate of #22063

Thank you @whzx5byb! I assumed this was suggested before, but could not find it. I'll close this issue and add a comment with my examples as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants