When using import, the definition of the imported value is copied to the current file
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 275
- PR merge metrics
- No merged PRs in 30d
Description
I encountered this issue while applying tsup to my project, and I'll show you a simple code example that you can reproduce.
I have two source files.
```ts
// src/index.ts
import func from "./func";
export default function run() {
return func;
}
```
```ts
// src/func.ts
export default class func {}
```
I use the build command like this `tsup src --clean --dts`. In this case, the build result is index.js, which looks like this:
```js
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => run
});
module.exports = __toCommonJS(src_exports);
// src/func.ts
var func = class {
};
// src/index.ts
function run() {
return func;
}
```
The func class is also included in the build output, func.js. Because of this, the func class in the return value of run in index.js and the func class in the func.js file will be different values, causing my test to fail.
If I replace index.ts and func.ts with js extensions and run them with tsx to test, I don't have this problem. I'm wondering if this is intended and if not, if there is a way to work around it.
Contributor guide
Research direction
Start by reproducing the issue with src/index.ts and src/func.ts using `tsup src --clean --dts`, then compare the generated index.js and func.js outputs. Trace how TypeScript imports are bundled and determine whether the duplicated class identity is intended. Done means the behavior is documented, corrected, or a supported workaround is identified and verified against this example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100