Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/haines/buf-cjs-esm-ts
https://github.com/haines/buf-cjs-esm-ts
Last synced: 29 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/haines/buf-cjs-esm-ts
- Owner: haines
- Created: 2024-02-16T13:02:25.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-26T21:06:44.000Z (9 months ago)
- Last Synced: 2024-04-29T01:52:54.654Z (7 months ago)
- Language: TypeScript
- Homepage: https://github.com/bufbuild/protobuf-es/issues/718
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TypeScript incorrectly loads CJS types for `@bufbuild/protobuf` with `NodeNext` module resolution
This repo demonstrates an issue with the dual-package setup of `@bufbuild/protobuf`.
There is a shared library, `lib`, which contains generated code to be consumed by the application, `app`.
`app` is compiled with `ESNext` module and `Bundler` module resolution, and TypeScript correctly resolves the types from the ESM directory within `@bufbuild/protobuf`.
`lib` is compiled with `NodeNext` module and module resolution, and TypeScript incorrectly resolves the types from the CJS directory within `@bufbuild/protobuf`.
This leads to type errors when `app` attempts to consume messages from `lib`.
```console
$ npm install$ npm run build
app/src/index.ts:8:6 - error TS2345: Argument of type 'Pet' is not assignable to parameter of type 'Message'.
Types of property 'fromJson' are incompatible.
Type '(jsonValue: JsonValue, options?: Partial | undefined) => Pet' is not assignable to type '(jsonValue: JsonValue, options?: Partial | undefined) => Message'.
Types of parameters 'options' and 'options' are incompatible.
Type 'Partial | undefined' is not assignable to type 'Partial | undefined'.
Type 'Partial' is not assignable to type 'Partial'.
Types of property 'typeRegistry' are incompatible.
Type '(import("./node_modules/@bufbuild/protobuf/dist/esm/type-registry").IMessageTypeRegistry & Partial) | undefined' is not assignable to type '(import("./node_modules/@bufbuild/protobuf/dist/cjs/type-registry").IMessageTypeRegistry & Partial) | undefined'.8 ohNo(new Pet());
~~~~~~~~~Found 1 error.
```I think this is due to [this TypeScript issue](https://github.com/microsoft/TypeScript/issues/50466).
You can see in VS Code that within `app/src/index.ts`, "Go to definition" on the `Message` type takes you to `node_modules/@bufbuild/protobuf/dist/esm/message.d.ts`, whereas within `lib/src/pet/v1/pet_pb.ts`, you go to `node_modules/@bufbuild/protobuf/dist/cjs/index.d.ts`.