1

I'm building a typescript project and I'm trying to do the very simplest thing as a first step with Polkadot: Connect to a node and get account information. However, when I run tsc, it totally blows up.

I've read all kinds of stuff in their docs and online about type generation and importing these "augmentations" (including this SO question), but I haven't managed to break through yet.

On top of that, it seems crazy to me that you would actually have to do this complex type generation step to make the library work, so I wanted to gut check it and see if anyone maybe has a super simple typescript repo out there that I can use as an example. I did see this example, but its wildly out of date, and it also doesn't have any explanation to go along with it.

For the benefit of googlers, here are a few of the errors I'm getting (of 127 in total):

node_modules/@polkadot/api-derive/accounts/flags.d.ts:3:15 - error TS2305: Module '"@polkadot/types/lookup"' has no exported member 'PalletElectionsPhragmenSeatHolder'.

3 import type { PalletElectionsPhragmenSeatHolder } from '@polkadot/types/lookup';
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@polkadot/api-derive/accounts/identity.d.ts:4:15 - error TS2305: Module '"@polkadot/types/lookup"' has no exported member 'PalletIdentityRegistration'.

4 import type { PalletIdentityRegistration } from '@polkadot/types/lookup';

And here's the entire contents of what I'm trying to compile:

import * as Polkadot from "@polkadot/api";

const provider = new Polkadot.WsProvider("wss://westend-rpc.polkadot.io");
export const client = Polkadot.ApiPromise.create({ provider });

// TODO: use the client to do something

2 Answers 2

1
// each chain is different in what they expose runtime-wise
// enable it on a static basis (westend being closest to polkadot)
import "polkadot/api-augment/polkadot";

import * as Polkadot from "@polkadot/api";

const provider = new Polkadot.WsProvider("wss://westend-rpc.polkadot.io");
export const client = Polkadot.ApiPromise.create({ provider });

// TODO: use the client to do something

As per the linked SO answer - metadata describes the chain, these types are only available at runtime, each chain is vastly different, so you need to add the per-chain types when using TS.

(This is unlike anything else where the interfaces are fixed, Substrate-based chains are lego pieces, all from different sources, the static pre-compile environment needs to know what happens at runtime)

2
  • I'm assuming you meant import "@polkadot/api-augment/polkadot"; with the @ sign? Adding that line ups the error count from 127 to 767.... I must be doing something wrong here.
    – kael
    Aug 31, 2022 at 19:55
  • Just as a gut check, if you create a small repo with the above file in and run tsc, does it compile properly for you? If so, I would greatly appreciate if you'd publish it so I can see what the difference is.
    – kael
    Aug 31, 2022 at 20:45
-1

The problem ended up being a tsconfig option: compilerOptions.skipLibCheck. I have that set to false in my go-to configuration. Switching it to true fixes the issue.

I have to say, though, this feels like a red flag. If I can only get this to work by turning off huge portions of typescript's consistency validation system, I'm not sure there's much of a point to using typescript. In fact, using typescript with part of its validation turned off would give you a false sense of security that can lead to innumerable bugs and errors at runtime.

For anyone finding this in the future, I think this raises serious concerns for building safe production applications....

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 3, 2022 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.