ipfs / helia

An implementation of IPFS in JavaScript
https://helia.io
Other
811 stars 81 forks source link

How to use PubSub on TypeScript ? #546

Open acsses opened 1 month ago

acsses commented 1 month ago

How to use PubSub on TypeScript ?

I want to use PubSub on Typescript and try to use the sample code on this page

https://helia.io/modules/_helia_ipns.html

However when I run the code, Some issues emerged. These error is below .These error happend during compile .

src/main.ts:347:5 - error TS18048: 'libp2pOptions.services' is possibly 'undefined'.

347     libp2pOptions.services.pubsub = gossipsub()
        ~~~~~~~~~~~~~~~~~~~~~~

src/main.ts:363:11 - error TS18046: 'helia.libp2p.services.keychain' is of type 'unknown'.

363     await helia.libp2p.services.keychain.importKey("self_key_pair",exported,user_name)
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/main.ts:368:18 - error TS2345: Argument of type 'HeliaLibp2p<Libp2p<ServiceMap>>' is not assignable to parameter of type 'PubsubRoutingComponents'.
  The types of 'libp2p.services' are incompatible between these types.
    Property 'pubsub' is missing in type 'ServiceMap' but required in type '{ pubsub: PubSub<PubSubEvents>; }'.

368           pubsub(helia)
                     ~~~~~

  node_modules/@helia/ipns/dist/src/routing/pubsub.d.ts:10:13
    10             pubsub: PubSub;
                   ~~~~~~
    'pubsub' is declared here.

src/main.ts:375:9 - error TS2322: Type 'HeliaLibp2p<Libp2p<ServiceMap>>' is not assignable to type 'HeliaLibp2p<Libp2p<DefaultLibp2pServices>>'.
  Type 'Libp2p<ServiceMap>' is not assignable to type 'Libp2p<DefaultLibp2pServices>'.
    Type 'ServiceMap' is missing the following properties from type 'DefaultLibp2pServices': autoNAT, dcutr, delegatedRouting, dht, and 5 more.

375         helia: helia,
            ~~~~~

  src/main.ts:34:5
    34     helia:HeliaLibp2p,
           ~~~~~
    The expected type comes from property 'helia' which is declared here on type 'peer_ctx'

Found 4 errors in the same file, starting at: src/main.ts:347