sdk-js icon indicating copy to clipboard operation
sdk-js copied to clipboard

feat!: namespaces for type augmentation

Open rflechtner opened this issue 2 years ago • 4 comments

fixes KILTProtocol/ticket#2224

Originally I was trying to fix type errors coming from types that are defined e.g. in @polkadot/api-base or @polkadot/api-derive and prevent us from disabling skipLibCheck. I made no real progress there but while working on it found moonbeams solution to augmenting their api which at least provides a nice template for how we can use augmentation with multiple chains. Putting this here as a WIP to be picked up later. @ntn-x2 I thought you'd probably be interested.

Right now you can only switch between different augmentations by changing an entry in the tsconfig.

Checklist:

  • [ ] I have verified that the code works
  • [ ] I have verified that the code is easy to understand
    • [ ] If not, I have left a well-balanced amount of inline comments
  • [ ] I have left the code in a better state
  • [ ] I have documented the changes (where applicable)

rflechtner avatar Nov 23 '22 17:11 rflechtner

Cool! I also saw how moonbeam does it, and tried to replicate it, but I was somehow having a duplicate definition error, since two different packages where trying to declare the @polkadot/types namespace. If that is fixed in this PR (which I haven't yet checked, will do some time this week), then this definitely represents a step in the right direction.

ntn-x2 avatar Nov 28 '22 06:11 ntn-x2

Cool! I also saw how moonbeam does it, and tried to replicate it, but I was somehow having a duplicate definition error, since two different packages where trying to declare the @polkadot/types namespace. If that is fixed in this PR (which I haven't yet checked, will do some time this week), then this definitely represents a step in the right direction.

I don't think it's completely fixed unfortunately, at least it does through related errors when you disable skipLibCheck if I remember correctly. This still needs a bit of work, but I think some issues might be resolved by a separate line of typescript compilation improvements I was pushing forward.

rflechtner avatar Nov 28 '22 11:11 rflechtner

@rflechtner I pushed a commit to make sure we don't default to spiritnet when importing @kiltprotocol/augment-api. It's my personal opinion that we should be explicit about this. Anyway, I don't think I have made much progress, since:

  • The project compiles with yarn build, but fails to be checked with yarn check
  • As soon as the peregrine namespace is imported in some files (e.g., public credentials), apparently it's not possible to remove those declarations even by manually importing @kiltprotocol/augment-api/spiritnet in a test script that simply imports the SDK

ntn-x2 avatar Dec 14 '22 16:12 ntn-x2

  • As soon as the peregrine namespace is imported in some files (e.g., public credentials), apparently it's not possible to remove those declarations even by manually importing @kiltprotocol/augment-api/spiritnet in a test script that simply imports the SDK

Yes, that is the reason you can't make any explicit imports anywhere in the repo. You will never be able to import spirit net types in one file and peregrine types in another as they will clash. The strategy I found to be somewhat working is to change the resolution of @kiltprotocol/augment-api in the tsconfig, that is a global thing though. I think this commit needs to be reverted because of the issues I tried to describe, but we can look into how we can avoid using defaults in the augment-api package.

rflechtner avatar Dec 15 '22 15:12 rflechtner

@rflechtner Can we close this now?

Dudleyneedham avatar Jul 24 '24 14:07 Dudleyneedham

Closing as superseded by https://github.com/KILTprotocol/types-augment

rflechtner avatar Jul 24 '24 14:07 rflechtner