-
Notifications
You must be signed in to change notification settings - Fork 22
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
Default import still broken with ESM and typescript moduleResolution node16 #61
Comments
Thank you for the reproduction steps @apottere ! I'll take a look at this shortly 👍🏼 |
The error gets fixed by Just moving the import ShortUniqueId from './short-unique-id.js'; So this tells me the error from the above steps has more to do with module or directory context. But, @apottere , since you mention you get an error when building your project, if you could provide an example project with the build tooling and steps you use, that would be most helpful. In the mean-time I'll create a dummy project on my end to try and reproduce the error based on the info given so far, but given the symptoms, it's looking more and more like an edge case related to a specific set of tools and configurations, specially given the broad range of projects I've been successfully using short-unique-id on as well as projects successfully depending on it 🤔 Cheers |
I've been able to reproduce the error: https://github.com/jeanlescure/suid-not-constructable-repro After trying most combinations of config, it seems the only variable(s) affecting this is the usage of Given that Node 16 reached end-of-life on September 11th, 2023, I'll just add a note on the README about the workaround: import ShortUniqueId from 'short-unique-id';
const suid = new ShortUniqueId.default({length: 3}); // add .default
console.log(suid.rnd()); |
Node 16 is EOL, but the node16 (aka nodenext) typescript module resolution is the most current and recommended setting. It's not the module resolution for node 16, it's the module resolution introduced in node 16. It's a terrible name for the setting, and has caused confusion before. Docs for reference:
The only other modern option would be |
And a note about the workaround: While that allows typescript to compile, arethetypeswrong claims:
If you want a workaround that definitely works, here's what I'm using: import ShortUniqueId from 'short-unique-id';
const shortCodeGenerator = new (ShortUniqueId as unknown as typeof ShortUniqueId.default)({
// ...
}); This uses the constructor from the default export as intended, but appeases typescript by telling it that |
I just tried Ok, back to the drawing board in finding a possible fix from our end, but this is definitely a false type error detection coming from upstream via typescript; undoubtedly proven by the example you just provided 👏🏼 |
After digging into it some more, I don't think there's a way to get a single file that retains what seems to be the desired behavior of the javascript (IIFE w/ esbuild) and working types with moduleResolution: node16. It seems like there are two options:
|
@andrewbranch posted the fix on this issue: ajv-validator/ajv#2132 (comment) It was just a matter of declaring the namespace appropriately in the import type {
default as ShortUniqueIdCore,
ShortUniqueIdRanges,
ShortUniqueIdRangesMap,
ShortUniqueIdDefaultDictionaries,
ShortUniqueIdOptions,
DEFAULT_UUID_LENGTH,
DEFAULT_OPTIONS
} from './short-unique-id-core.d.ts';
declare namespace ShortUniqueId {
export {
ShortUniqueIdRanges,
ShortUniqueIdRangesMap,
ShortUniqueIdDefaultDictionaries,
ShortUniqueIdOptions,
DEFAULT_UUID_LENGTH,
DEFAULT_OPTIONS
}
}
declare class ShortUniqueId extends ShortUniqueIdCore {}
export = ShortUniqueId; The I just released Cheers 🥂 |
Oh perfect, I saw namespaces as a potential solution but didn't look into it. Looks like Thanks for the fix 🥂 |
#59 was closed as complete, but the issue remains in the latest release version and also in trunk.
https://arethetypeswrong.github.io/?p=short-unique-id%405.1.1
With the newly added specs, I was able to confirm the error shown on the page above by making the following changes to the
specs
directory:specs/package.json
specs/import-esm.spec.ts
to include.js
in the import:Then, running
npx tsc
in that directory produces the following error:I also confirmed that my project still has trouble building with the latest release version ([email protected])
The text was updated successfully, but these errors were encountered: