sciencesakura / mutf-8

An encoder/decoder for Modified UTF-8 which is used in the Java platform such as the class file format and the object serialization.
MIT License
7 stars 1 forks source link

ESM TypeScript Resolution #17

Closed Offroaders123 closed 7 months ago

Offroaders123 commented 10 months ago

Was looking into using this project with my own Minecraft NBT parser, which is built to target ESM TypeScript. I'm not completely certain it was ESM module resolution that specifically caused the issue, but installing this project to my dependencies, TypeScript wasn't able to resolve the types accordingly.

After looking into the syntax that allows dual-packages to define ESM and CJS targets, I thought I'd try this dual-types description type. This indeed fixed the problem, I think it wasn't working before because TypeScript couldn't find type aliases from the export key, so maybe it can't use types in conjunction with exports.

https://stackoverflow.com/questions/58990498/package-json-exports-field-not-working-with-typescript

This was how I discovered this issue:

$ npm install mutf-8
{
  "type": "module"
}
import { MUtf8Encoder } from "mutf-8";
// Could not find a declaration file for module 'mutf-8'. './node_modules/mutf-8/dist/index.mjs' implicitly has an 'any' type.
//  There are types at './node_modules/mutf-8/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'mutf-8' library may need to update its package.json or typings. ts(7016)
Offroaders123 commented 10 months ago

Oops, I didn't realize that future commits also go towards the pull request, I was only looking to submit the first one. I'm going to look into how to fix that. The other commits were just a few experiments I wanted to try out.

sciencesakura commented 7 months ago

Thanks 😄