Skip to content
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

Better implicit function expression #25501

Closed
4 tasks done
KSXGitHub opened this issue Jul 8, 2018 · 5 comments
Closed
4 tasks done

Better implicit function expression #25501

KSXGitHub opened this issue Jul 8, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Jul 8, 2018

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)
@kitsonk
Copy link
Contributor

kitsonk commented Jul 8, 2018

Your assumption is that the initial argument type infers the return of the function. That is not a valid assumption. For example, the following is totally valid:

const fn = x => 2;

But the type of x is irrelevant.

What about:

const fn = (x, y) => y;

Again, totally valid.

There simply is not sufficient information to reliably infer a return type for functions authored this way. TypeScript is good, but it cannot read minds of the developers (yet).

@KSXGitHub
Copy link
Contributor Author

KSXGitHub commented Jul 8, 2018

@kitsonk Simple, just make x: any.

Your assumption is that the initial argument type infers the return of the function

Actually, my assumption is that initial arguments will be used (x is passed to bar in my code snippets).

@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #17428 and #14078.

@DanielRosenwasser
Copy link
Member

Actually, looking a little closer, it seems more like this is an ask for non-local type inference, not making unannotated functions generic.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 9, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants