thesayyn / protoc-gen-ts

Compile protocol buffer messages to TypeScript.
MIT License
362 stars 75 forks source link

Property 'Buffer' does not exist on type 'typeof globalThis' #247

Open yijiebaiyi opened 11 months ago

yijiebaiyi commented 11 months ago

Automatically generating ts files is successful, but running npm run build reports an error: Property 'Buffer' does not exist on type 'typeof globalThis'

The code snippet where the error occurred:

function base64FromBytes(arr: Uint8Array): string {
  if (globalThis.Buffer) {
    return globalThis.Buffer.from(arr).toString("base64");
  } else {
    const bin: string[] = [];
    arr.forEach((byte) => {
      bin.push(globalThis.String.fromCharCode(byte));
    });
    return globalThis.btoa(bin.join(""));
  }
}

the proto message has type bytes:

message Event {
    string department        = 20; 
    string job               = 21;  
    string picturePath       = 22; 
    bytes imageData          = 23; 
    string createdAt         = 24; 
    uint32 keyPartType       = 25; 
}

my env: node version: v14.18.3 protoc version: 3.13.0 protoc-gen-ts version: 0.8.7

what should I do?

yijiebaiyi commented 11 months ago

make some additions

ts-node version: 8.6.2 tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}
thesayyn commented 9 months ago

This sounds like a typechecking issue, as i never tested the rust implementation for type errors.