DaniGuardiola / rpc-anywhere

Create a type-safe RPC anywhere.
https://rpc-anywhere.dio.la
MIT License
169 stars 2 forks source link

Property 'addMessageListener' does not exist on type 'RPC<IframeSchema, ParentSchema>' #28

Open phanmn opened 5 months ago

phanmn commented 5 months ago

In which versions of the package (and relevant enviroment tools such as Node.js) have you observed the bug?

1.7.0

What steps will reproduce the bug?

Follow README

What behavior did you expect?

No ts error

What actually happened?

Property 'addMessageListener' does not exist on type 'RPC<IframeSchema, ParentSchema>'

Workaround

No response

Proposed fix

No response

Other remarks

No response

Contribution

DaniGuardiola commented 5 months ago

I can't reproduce this issue. Did you copy the iframe example as-is? Here's a TypeScript playground I created that shows it all working: https://tsplay.dev/m3dpyW

DaniGuardiola commented 5 months ago

This might be relevant to you: docs

The schema types affect the rpc instance types. If the remote schema type doesn't declare any messages it can send, then your local rpc instance type won't have an addMessageListener method, since there's no message to listen to. The README example does have it though, so I'm not sure what's going on in your case.

It would easier for me to debug if you could share a more complete reproduction of the issue, using the TypeScript playground or a sandbox service like Stackblitz

phanmn commented 5 months ago

Thanks @DaniGuardiola , I will try to give you reproduction soon

phanmn commented 4 months ago

I found the reason, because the tsconfig.json

My original config (from nuxt3)

{
  // https://nuxt.com/docs/guide/concepts/typescript
  "extends": "./.nuxt/tsconfig.json",
  "vueCompilerOptions": {
    "plugins": ["@vue/language-plugin-pug"]
  },
  "compilerOptions": {
    "sourceMap": true,
    "strict": false,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "types": []
  }
}

Missing config

"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"declaration": true,
"target": "ES2017",
"module": "ESNext"
DaniGuardiola commented 4 months ago

Interesting. Could you find the specific config value that makes it break/work? It'd help to get a narrowed-down version so I can investigate.