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

Difference between Javascript code generated from Typescript between esbuild and tsc #1596

Closed
sloonz opened this issue Sep 11, 2021 · 2 comments

Comments

@sloonz
Copy link

sloonz commented Sep 11, 2021

Typescript :

class Test {
    id?: number;
}

Code generated by tsc :

class Test {
}

Code generated by esbuild :

class Test {
    id;
}

This results in an observable difference for console.log({...new Test()}) : tsc version results in {}, esbuild version in {id: undefined}. This difference breaks, for example, the .patch() method of Objection, because patch() does something like this : initialObject.$set(partialUpdate), which overrides non-patched fields to undefined (instead of preserving previous values).

@hyrious
Copy link

hyrious commented Sep 11, 2021

This behavior is controlled by tsconfig: useDefineForClassFields, which is enabled by default in esbuild if:

  • bundle
  • there is target: esnext in tsconfig

In other words, it is disabled if

  • transform --target=esnext

If you need this behavior in transform, you have to append tsconfig like this:
--tsconfig-raw={"compilerOptions":{"useDefineForClassFields":true}}.

I have suggested enable it in transform if target is esnext in another issue: #1480 (comment)

@sloonz
Copy link
Author

sloonz commented Sep 12, 2021

Awesome, thanks for the answer !

@sloonz sloonz closed this as completed Sep 12, 2021
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