I'm using buf with protoc-gen-es to generate protobuf TS. tsc and vscode are happy with that generated code. However, that generated code imports types from @bufbuild/protobuf and the generated types extend those types. When I run tsc it errors on methods inherited from @bufbuild/protobuf.
Some of my code:
// BusMessage is a generated type
let bm = new akpb.BusMessage();
// type is a property on BusMessage; tsc likes this
bm.type = akpb.ExternalMessageType.SUBSCRIBE;
// toBinary is inherited from types in @bufbuild/protobuf
// TS2339: Property 'toBinary' does not exist on type 'BusMessage'.
this.#socket.send(bm.toBinary());
This is for the browser and I'm trying to use ES modules. protobuf-gen-es defaults to ES-style modules/imports.
Here's the generated code doing its imports:
import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
import { Message, proto3 } from "@bufbuild/protobuf";
Here's my tsconfig.json:
{
"compilerOptions": {
"module": "es2022",
"noImplicitAny": true,
"target": "es2018"
},
"include": [
"src/*ts"
],
}
and my package.json:
{
"name": "ak",
"main": "main.js",
"type": "module",
// ...
"dependencies": {
"@bufbuild/buf": "^1.32.1",
"@bufbuild/protobuf": "^1.9.0",
"@bufbuild/protoc-gen-es": "^1.9.0"
},
"devDependencies": {
"typescript": "^5.4.5",
// ...
}
}
My code lives in ./src/ relative to ./package.json and ./tsconfig.json.
I'm very experienced in other languages and understand the basics of JS and TS, but am totally new to TS tooling.