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

TSX Loader fails to parse Generics for Arrow Functions #265

Closed
macarie opened this issue Jul 16, 2020 · 2 comments
Closed

TSX Loader fails to parse Generics for Arrow Functions #265

macarie opened this issue Jul 16, 2020 · 2 comments

Comments

@macarie
Copy link

macarie commented Jul 16, 2020

Hi 🙈

I've found this bug/weird (not really) behaviour: when generics are being used with arrow functions while using the tsx loader, ESBuild will throw an error.

Reproducible with:

import esbuild from 'esbuild'

const ts = `
export const useless = <T>(array: T[]): T[] => {
	return array
}
`


esbuild.transformSync(ts, {
	loader: 'tsx',
})

// => Fails with: <stdin>:3:1: error: Unexpected "return"

This seems to be a known issue with TypeScript as well (microsoft/TypeScript#4922), so I was a bit hesitant whether to report it or not.

Do you think that this can be handled somehow by ESBuild?

By the way, thank you for this amazing project 🙂


A workaround for anyone landing here:

export const useless = <T,>(array: T[]): T[] => {
	return array
}
export const useless = <T extends unknown>(array: T[]): T[] => {
	return array
}
@evanw
Copy link
Owner

evanw commented Jul 16, 2020

The tsx loader intentionally follows TypeScript's behavior since that's basically the "specification" for how TypeScript is supposed to work. So if that's the way the TypeScript compiler works, then esbuild should work the same way and this is not a bug in esbuild. If you want this behavior to be changed you'll have to get it changed in the official TypeScript compiler first, at which point it will become a bug in esbuild because they will behave differently.

I'm also not going to try to turn esbuild into a "better TypeScript" because it wouldn't do any good. The point of TypeScript is the types, and with esbuild you still have to run the official TypeScript compiler to do type checking. So even if esbuild could parse this syntax when TypeScript can't, that means you'd no longer be able to type check your source code.

@evanw evanw closed this as completed Jul 16, 2020
@macarie
Copy link
Author

macarie commented Jul 16, 2020

Understood, thank you anyway 👍🏼

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