-
Notifications
You must be signed in to change notification settings - Fork 18
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
TypeScript issues with moduleResolution: "nodenext"
and declaration: true
#18
Comments
moduleResolution: "nodenext"
breaks typesmoduleResolution: "nodenext"
and declaration: true
Hi @eugene1g, Thank for reaching out. I am having issues reproducing this. Which version of TypeScript are you using? Also, would switching |
Hi @ehmicky I forgot to say "thank you!" for the awesome library. Changing |
The core issue here is when the application uses This patch will do that - it tweaks diff --git a/src/main.d.ts b/src/main.d.ts
index 2da7b22..1ce8f22 100644
--- a/src/main.d.ts
+++ b/src/main.d.ts
@@ -7,6 +7,8 @@ export type { InstanceOptions } from './options/instance.js'
export type { MethodOptions } from './options/method.js'
export type { Info } from './plugins/info/main.js'
export type { Plugin } from './plugins/shape/main.js'
+export type { CustomClass } from './subclass/custom.js'
+export type { ErrorSubclassCore } from './subclass/create.js'
export type { ErrorClass }
/**
diff --git a/src/subclass/create.d.ts b/src/subclass/create.d.ts
index 3d6a65e..02712a0 100644
--- a/src/subclass/create.d.ts
+++ b/src/subclass/create.d.ts
@@ -20,7 +20,7 @@ import type { NormalizeError } from './normalize.js'
/**
* `ErrorClass.subclass()`
*/
-type CreateSubclass<
+export type CreateSubclass<
PluginsArg extends Plugins,
ErrorPropsArg extends ErrorProps,
CustomClassArg extends CustomClass, |
Hi @eugene1g, Sorry for the late reply. @all-contributors Please add @eugene1g for code, bug. |
I couldn't determine any contributions to add, did you specify any contributions? I couldn't determine any contributions to add, did you specify any contributions? I've put up a pull request to add @eugene1g! 🎉 |
Hi @ehmicky , I started using Modern Error plugins, and discovered that we need one more type, The fix would be to export
|
Hi, it might just be something weird about my setup but I'm getting the same TS2742 error when using the export const UserError = BaseError.subclass('UserError', {
custom: class extends BaseError {
constructor(message: string, options?: InstanceOptions & UserErrorOptions) {
// do stuff...
super(message, options)
}
},
}) Exporting a couple more types, diff --git a/build/src/main.d.ts b/build/src/main.d.ts
index a17f0e34534a9a5c1a8fe2c55bc2ef964f65ef8b..63413eab64ab41b933a6f39f731b00a12e86efc4 100644
--- a/build/src/main.d.ts
+++ b/build/src/main.d.ts
@@ -2,6 +2,7 @@ import type {
ErrorClass,
SpecificErrorClass,
ErrorSubclassCore,
+ CreateSubclass,
} from './subclass/create.js'
import type { CustomClass } from './subclass/custom.js'
@@ -22,7 +23,9 @@ export type {
// https://github.com/ehmicky/modern-errors/issues/18
CustomClass,
ErrorSubclassCore,
+ CreateSubclass,
}
+export type { NormalizeError } from './subclass/normalize.js'
/**
* Top-level `ErrorClass`.
diff --git a/build/src/subclass/create.d.ts b/build/src/subclass/create.d.ts
index 3d6a65eebd807c761e23bb0e5065b0c6c6063fa6..02712a03c1e41a4aa8aa9d687a8b981cb9b2a92d 100644
--- a/build/src/subclass/create.d.ts
+++ b/build/src/subclass/create.d.ts
@@ -20,7 +20,7 @@ import type { NormalizeError } from './normalize.js'
/**
* `ErrorClass.subclass()`
*/
-type CreateSubclass<
+export type CreateSubclass<
PluginsArg extends Plugins,
ErrorPropsArg extends ErrorProps,
CustomClassArg extends CustomClass, Please let me know if you want me to create a new issue with a reproduction and/or create a pull request. Thanks. |
Hi @eugene1g, Thanks for reaching out. |
Sure, @ehmicky, here's the repro link. Please note, however, that the link doesn't recreate the error when I first visit it, which is weird, I have to restore the config shown in the pics below, and toggle declarations on and off, then the lint error eventually appears: ![]() ![]() UPDATE: this stackblitz link shows the problem more reliably: Seems to be the same issue, whereby Great library BTW, my errors have never been so nicely organised! |
Thanks a lot for reporting this, that's very helpful! I can reproduce this problem, and can see that your fix would work. What I'm trying to figure out is: why is this currently failing? So this problem does not appear again in the future. Also, exporting types that are not meant to be public, as a workaround for a bug, might not be as good as understanding the underlying problem, and fixing that instead. The project has a wide array of type tests, especially on the modern-errors/src/subclass/custom.test-d.ts Lines 183 to 189 in 7d2f04a
Changing the Would you like to submit a PR with your above fix? Thanks again! |
Hi @ehmicky, it is probably related to this open issue on TS itself: It's a very weird error in that a It might be worth waiting to see what the official solution for that TS issue is as, I agree, exposing private types is less than ideal, and it'll be a never ending problem. For example, I was messing around extending the inferred modern-errors type in various other ways and I encountered the same TS2742 issue, with the fix being to expose yet more private types! That said, I haven't encountered this error with other packages, so perhaps there is something odd about the modern-errors setup? Might the title of the TS issue be a clue: "... occurs when multiple modules with the same package ID are resolved"? When I look in Might a solution be to figure out why the packages are installed both at the root-level and in the nested |
Very good find, this does appear to be the TypeScript issue! Modern errors uses plugins like Then, modern-errors/src/plugins/core/all.test-d.ts Lines 1 to 10 in 7d2f04a
Since Do you think the following fix would work? |
Yeah, I tried to use that fix before reporting the issue to you, but I couldn't get it to work. I might have misunderstood how to use the fix though. I tried making all the modern-errors deps, deps of my own project, and then importing their types like so: import type {} from 'error-class-utils'
import type {} from 'error-custom-class'
import type {} from 'filter-obj'
import type {} from 'is-plain-obj'
import type {} from 'merge-error-cause'
import type {} from 'normalize-exception'
import type {} from 'set-error-message'
import type {} from 'set-error-props'
import type {} from 'set-error-stack' That didn't work though. How would you interpret the fix? |
I was also unclear about what they meant. Modern errors is definitely doing some weird shenanigans. The types are quite complex and highly recursive. I wrote the following comments summarizing it. Lines 43 to 88 in 7d2f04a
One key problem is that It is a shame though to not be able to use the |
Yeah, you're definitely using some TS voodoo magic that goes right over my head! My guess is that the problem is related to that underlying TS issue, and that modern-errors is susceptible to it because of:
I'm not sure I can usefully contribute to a fix as I'm a bit out of my depth! But I do have a workaround that works for me, and that's simply to use PNPM's built in patching mechanism to add exports of any private types that are causing problems. I guess other TS users will just have to do the same if they want to use the Either that, or I can submit a PR to add those exports with a comment making it clear that they're only exported as a temporary measure until the underlying TS issue has a fix. I could export all the private types individually, with a JSDoc comment that'll mark them as deprecated in IDE tooling, e.g.: import type { NormalizeError } from './subclass/normalize.js'
/**
* @deprecated This type is private and only exported as a temporary workaround
* for an open issue with TypeScript (see {@link https://github.com/microsoft/TypeScript/issues/47663|TypeScript issue #47663}).
* It will be removed in a future release.
*/
export type NormalizeError Your call, happy to raise the PR. |
Yes, it does seem to be the way you describe it. If you'd like to submit the above fix, that'd be great! |
Released in |
Installed and working! Nice doing business with you! 🙏 |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [modern-errors](https://togithub.com/ehmicky/modern-errors) | dependencies | patch | [`7.0.0` -> `7.0.1`](https://renovatebot.com/diffs/npm/modern-errors/7.0.0/7.0.1) | --- ### Release Notes <details> <summary>ehmicky/modern-errors (modern-errors)</summary> ### [`v7.0.1`](https://togithub.com/ehmicky/modern-errors/blob/HEAD/CHANGELOG.md#701) [Compare Source](https://togithub.com/ehmicky/modern-errors/compare/7.0.0...7.0.1) #### Types - Fix TypeScript types when using the `custom` option [https://github.com/ehmicky/modern-errors/issues/18](https://togithub.com/ehmicky/modern-errors/issues/18)s/18). </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 4am on the first day of the month" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjMzMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJhdXRvbWVyZ2UiXX0=-->
Guidelines
Describe the bug
When using TypeScript with
"moduleResolution: "nodenext"
and"declaration": true"
, it fails to compile the example fromREADME
Steps to reproduce
main.ts
tsconfig.json
error:
I can make it work in two ways -
declaration
. However, this means my library cannot publish types and that's not great."moduleResolution": "node"
. However, this means I cannot use theexports
settings in my dependencies.With
"moduleResolution":"node"
it generates these types formain.d.ts
:Which hints at the reason for failure: TypeScript has to tap into internal files to reference un-exported types to get
ErrorSubclassCore
(and, in my real project,CustomClass
). With"moduleResolution":"node"
, this deep linking is not possible, so the TS compiler gives up.Environment
Pull request (optional)
The text was updated successfully, but these errors were encountered: