3

I am trying to create my own derives using DeriveCustom like this:

import { DeriveCustom } from '@polkadot/api-derive';
import * as staking from './staking';

export const derive: DeriveCustom = {
  customStaking: staking as unknown as DeriveCustom[string]
};
import { derive as customDerive } from '@my-network/api-derive';
// ...
export const options = ({
// ..
derives: {
    ...customDerive,
  }
// ..
}

but when I try to use api.derive.customStaking, it is not recognized by typescript.

1

2 Answers 2

2

Take a look at this @polkadot/api issue. Basically the poster had the same question.

In this case it led to an enhancement making the derives augmentable (usage below copied from this PR) and that went out into the wild as part of the 7.9.1 release

// augmentDerives.ts
import type { Observable } from 'rxjs';

declare module '@polkadot/api-derive/derive' {
  // extend, add our custom section
  export interface ExactDerive {
    customPallet: {
      someMethod: ReturnType<() => () => Observable<number[]>>
    }
  }
}

EDIT: Giving credit where credit is due - @RosaryBeads did mention the exact same link in a comment above, I didn't see it when posting this... Keeping it here since my expansion on the usage as well as version adds enough additional context.

1
  • An example that I used is found in this custom derives module, imported with import custom from './custom-derives' and api constructed with const api = await ApiPromise.create({ derives: custom, provider });
    – datu-puti
    Commented May 30, 2023 at 11:10
1

As a workaround I cast to any:

await (api.derive as any).customDerive.method()

But this loses intellisense and type checking.

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.