Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

no-function-expression and generic functions in TSX #518

Closed
astorije opened this issue Oct 3, 2018 · 3 comments
Closed

no-function-expression and generic functions in TSX #518

astorije opened this issue Oct 3, 2018 · 3 comments
Labels
Difficulty: Easy Someone with little to no experience in TSLint should be able to send a pull request for this issue. Status: Accepting PRs Type: Bug
Milestone

Comments

@astorije
Copy link
Contributor

astorije commented Oct 3, 2018

While the following is supported by the language:

function foo<T>(x: T): T { return x; }

The following is not:

const foo = <T>(x: T) => x; // ERROR : unclosed `T` tag

no-function-expression encourages to use arrow functions, which can lead to the following to make the linter pass (or disable the linter on a case-by-case basis):

const foo = <T extends {}>(x: T) => x;

Which isn't great. See https://stackoverflow.com/questions/32308370/what-is-the-syntax-for-typescript-arrow-functions-with-generics

Similarly to the fact that function expressions that contain a this reference are allowed and will not create a failure, it would be nice if function expressions that have a generic type were allowed.

Thoughts?

@JoshuaKGoldberg
Copy link

The following is not:

const foo = <T>(x: T) => x; // ERROR : unclosed `T` tag

Are you on the latest TypeScript? That works fine for me on typescriptlang.org/play (link). Seems like that SO answer is outdated now?

@astorije
Copy link
Contributor Author

astorije commented Oct 4, 2018

Yes, latest TypeScript, but I believe this is a limitation of TSX. So the exclusion could be generic function in a TSX file.

@JoshuaKGoldberg JoshuaKGoldberg added Type: Bug Status: Accepting PRs Difficulty: Medium People with non-trivial experience in TSLint should be able to send a pull request for this issue. labels Oct 4, 2018
@JoshuaKGoldberg
Copy link

TSX

Ah! I'd missed that. Yes, agreed, this is a bug. +1 to excluding these generic functions in TSX files.

@JoshuaKGoldberg JoshuaKGoldberg added Difficulty: Easy Someone with little to no experience in TSLint should be able to send a pull request for this issue. Hacktoberfest and removed Difficulty: Medium People with non-trivial experience in TSLint should be able to send a pull request for this issue. labels Oct 4, 2018
JoshuaKGoldberg pushed a commit that referenced this issue Oct 7, 2018
* Ignore .tsx file for no-function-expression rule

* Add test case for excluding .tsx file
for no-function-expression rule

* Add logic to only allow generic type function expression
in .tsx file
@JoshuaKGoldberg JoshuaKGoldberg added this to the 6.0.0-beta0 milestone Nov 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Difficulty: Easy Someone with little to no experience in TSLint should be able to send a pull request for this issue. Status: Accepting PRs Type: Bug
Projects
None yet
Development

No branches or pull requests

2 participants