-
Notifications
You must be signed in to change notification settings - Fork 12.8k
tsc needlessly emits Object.defineproperty() line when "import" keyword is used. #47563
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
Comments
This is working as intended. As soon as you export or import something you're dealing with a module, and modules get this statement. |
Then, is there an alternative way? The #include directive in C language does not cause build/runtime failures. if (typeof exports !== `undefined`)
Object.defineproperty(exports, "__esModule", { value: true }); we can use tsc like what I want to do. |
If you want to use |
Duplicate of #42371 and a bunch of others? |
Thanks. But in my case, server.ts is for Node.js and client.ts is for browser. Can we specify different targets for server.ts and client.ts ? If I change "module": in my tsconfig.json from "commonjs" to "es2015" so that client.js does not contain the Object.defineproperty() line, server.js starts failing to run. If I also add "type": "module" to my package.json, "node server.js" complains
at import { Pool } from 'pg'; line. My server.ts contains import { Pool, PoolClient, QueryResult } from 'pg'; line, and I was able to change like import pg from 'pg';
const { Pool } = pg; for "Pool" as suggested, but I can't do similar for "PoolClient" and "QueryResult", and tsc complains about "PoolClient" and "QueryResult".
It seems yes. And just adding
would solve this problem? |
@KumanekoSakura For your particular use-case, you may just consider using I also feel like it's quite annoying that |
Although I wish tsc to emit
line immediately before
line, I found that modifying server.ts file to emit <script type='text/javascript'>
'use strict';
const exports = new Object();
</script> lines immediately before <script type='text/javascript' src='client.js'></script> line is a simple workaround which I can choose. |
They're discussing to remove this export: #47470 (comment) |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Hi, I had the same problem, it was solved with this video |
Bug Report
I want to share interface definitions between "a .ts file for server side" (say, server.ts) and "a .ts file for client side" (say, client.ts) using a shared .ts file (say, interfaces.ts) in order to be able to catch interface definition mismatch errors. I want to use interfaces.ts as if a .h file in C language.
server.js (which runs using Node.js + Express) emits a HTML response which contains
in order to let web browser parse client.js file.
But tsc command needlessly emits
line into client.js (which causes
error when parsed by a web browser) if I use "import" keyword from client.ts file.
As a result, currently I have to embed output of
into client.ts file.
Maybe this error could be avoided by specifying non-default values in my tsconfig.json (generated by "tsc --init"), but I am not familiar with JavaScript/TypeScript specifications.
🕗 Version & Regression Information
tsc 4.5.4
💻 Code
In C language, I can do like below.
In TypeScript language, I tried like below.
🙁 Actual behavior
tsc emits
line (and causes runtime error on the browser side) when "import" keyword is used.
🙂 Expected behavior
tsc does not emit
line. (Or, tsc makes sure that
error does not happen.)
The text was updated successfully, but these errors were encountered: