You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When bundling a library with parcel and relying on @parcel/transformer-typescript-types to generate a bundled .d.ts file, you'll get incorrect output if there is a naming conflict between things that are exported by a module consumed as a namespace import (e.g. import * as MyNamespace from "./foo.
When you build this project with parcel, and try to consume it, the types don't describe the runtime behavior. If you build the same library with tsc, you get correct types.
import*asParcelfrom"test-library-parcel";import*asTscfrom"test-library-tsc";// This library has the same source code, but it's built with tsc.it("Library built with tsc has types match runtime behavior",()=>{expect(Tsc.consumer1.messageFromOther1).toBe("foo");// Type: { messageFromOther1: string; }expect(Tsc.consumer2.messageFromOther2).toBe("foo");// Type: { messageFromOther2: string; }});it("Library built with parcel has types that do not match runtime behavior",()=>{// @ts-expect-errorexpect(Parcel.consumer1.messageFromOther1).toBe("foo")// Type: { messageFromOther2: string; }expect(Parcel.consumer2.messageFromOther2).toBe("foo")// Type: any});
Here is the index.d.ts file that parcel currently generates:
When we are resolving the namespaced names (e.g. NamespacedImport.nameConflict), we'll iterate through all the exports other1.ts, in order. The first export (export { nameConflict as notAConflict };) hit the recursive condition on line 146, which causes it to go looking for nameConflict in other2.ts, which it finds and returns. This is incorrect because we didn't consider that might rename the export in the process of re-exporting.
There might be more layers to the onion, too.
馃敠 Context
I was working on a PR add support for namespace exports to @parcel/transformer-typescript-types, and I ran across this issue in the process.
馃捇 Code Sample
You can run the above examples in the namespace-imports branch of this repo
馃實 Your Environment
Software
Version(s)
Parcel
2.6.2
Node
16.15.1
Yarn
1.22.19
Operating System
Windows 11 Version 10.0.22000 Build 22000
The text was updated successfully, but these errors were encountered:
馃悰 bug report
When bundling a library with parcel and relying on
@parcel/transformer-typescript-types
to generate a bundled.d.ts
file, you'll get incorrect output if there is a naming conflict between things that are exported by a module consumed as a namespace import (e.g.import * as MyNamespace from "./foo
.馃帥 Configuration (.babelrc, package.json, cli command)
package.json
src/index.ts
src/other1.ts
src/other2.ts
馃槸 Current Behavior
When you build this project with parcel, and try to consume it, the types don't describe the runtime behavior. If you build the same library with tsc, you get correct types.
Here is the
index.d.ts
file that parcel currently generates:types.d.ts
馃 Expected Behavior
The types should correctly describe the runtime behavior. This would be
index.d.ts
I would expect, were this to be fixed:馃拋 Possible Solution
I think at least part of the problem lies in the way that have implemented
resolveExport
:parcel/packages/transformers/typescript-types/src/TSModuleGraph.js
Lines 138 to 155 in 1a96d6d
When we are resolving the namespaced names (e.g.
NamespacedImport.nameConflict
), we'll iterate through all the exportsother1.ts
, in order. The first export (export { nameConflict as notAConflict };
) hit the recursive condition on line 146, which causes it to go looking fornameConflict
inother2.ts
, which it finds and returns. This is incorrect because we didn't consider that might rename the export in the process of re-exporting.There might be more layers to the onion, too.
馃敠 Context
I was working on a PR add support for namespace exports to
@parcel/transformer-typescript-types
, and I ran across this issue in the process.馃捇 Code Sample
You can run the above examples in the
namespace-imports
branch of this repo馃實 Your Environment
The text was updated successfully, but these errors were encountered: