-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
bug: using package exports fields can result in incompatible types for identical types #49466
Comments
Ok, I have discovered a way to easily reproduce the error with code we have already shipped in beta. Here are the instructions:
import { ArrayObserver, Observable } from "@microsoft/fast-element";
import { mergeSpliceStrategy } from "@microsoft/fast-element/splice-strategies";
const observer = Observable.getNotifier<ArrayObserver>([]);
observer.strategy = mergeSpliceStrategy; You will see the same type of error as above when attempting to set the This is a major ship-blocker for FAST 2.0. We really appreciate your assistance in resolving this issue. Please let me know if there's anything I can do to help. |
This is likely because you have two, distinct copies of If you have deep entrypoints, you really shouldn't be bundling you |
@weswigham Are you saying that if we point our root d.ts export to the non-bundled index.d.ts then the problems with just "magically" disappear? In other words, do we just change this: ".": {
"types": "./dist/fast-element.d.ts",
"production": "./dist/esm/index.js",
"development": "./dist/esm/index.debug.js",
"default": "./dist/esm/index.js"
}, Into this: ".": {
"types": "./dist/dts/index.d.ts", <-- just change this?
"production": "./dist/esm/index.js",
"development": "./dist/esm/index.debug.js",
"default": "./dist/esm/index.js"
}, |
Yep. (And as I've said, you probably don't have a great reason to even ship bundled declarations at all now that you're shipping multiple explicit entrypoints) |
Thanks @weswigham! I'll give this a try. It makes sense. I'll report back soon. |
A quick local test (updating some package.json in node modules folder) shows that this indeed gets it working beautifully. Again, huge thanks @weswigham !!! Closing issue as resolved. |
@weswigham QQ: If I'm using exports as above, should I also remove the "types" package.json field that previous also pointed to the rollup dts? Am I right in assuming that with exports configuration that the "types" field is no longer needed? Referring specifically to the "type" field at the package level, not the ones in the exports. |
Keep the |
Thank you! |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
This bug pertains to the new support for Node16 package exports.
⏯ Playground Link
A playground link will not demonstrate the issue because it only occurs when importing a second package's exports.
💻 Code
This PR demonstrates a set of code changes to the
@microsoft/fast-element
library which results in a package that cannot be properly consumed from TypeScript: microsoft/fast#6087The PR simply moves some code, which was originally part of the default export, and makes it an optional set of functionality exposed only through a special package export via
@microsoft/fast-element/binding/two-way
.This is an important scenario for us and a dependency of our upcoming release.
🙁 Actual behavior
As a test, I built the package, which builds fine. Then I created a new project where I could consume the package. Since we haven't published this package yet (or even merged the PR), I made a quick check by simply copying my local version of the package over into the
node_modules
folder of my test project. I then attempted to import via the path above. There is no problem importing. The problem is that the types between the default export and the special export don't match, according to the compiler. So, if you have a function that takes a certain type as an input, coming from the default export, and then the special export exports an implementor of that interface, you cannot pass the instance to the function. Here is a made up example of what I'm talking about:@foo/bar
index.ts
@foo/bar
cool-tasks.ts
@foo/bar
package.json
Usage in a Project
Note: I have not tried out this exact code, but this is essentially the pattern from our package with the PR linked above. We cannot pass the
twoWay
BindingConfig
const to thebind
function that takes an instanceBindingConfig
. TypeScript thinks that the types are incompatible, even though they are both referencing the same file in the package.Here is a screenshot of the real code in action:
This exact same consuming code works perfectly if I simply move the
twoWay
code back into the default export and import it from there. If you examine the PR above, you will see that all we have done is moved the code to its own file and specified a package export.🙂 Expected behavior
I would expect this to compile without issue.
The text was updated successfully, but these errors were encountered: