Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Use String as Enum Key - Auto-Converts to Text #6117

Open
AtelyPham opened this issue Mar 14, 2025 · 2 comments
Open

Unable to Use String as Enum Key - Auto-Converts to Text #6117

AtelyPham opened this issue Mar 14, 2025 · 2 comments

Comments

@AtelyPham
Copy link

Hi Polkadot-JS team! I’m running into a small hiccup and could use your help. When I try to define an enum with String as a key, it seems to get auto-converted to Text in the sanitize function (here: sanitize.ts#L31). I’d love to use String directly, but I’m not sure if I’m missing something!

Current Behavior:

Defining an enum with String throws an error:

Error: createType(FieldType):: Cannot map Enum JSON, unable to find 'String' in uint8, int8, text.

Expected Behavior:

I’d hope to use String (or any custom key) in my enum without conversion. If that’s not possible, maybe a quick note in the docs could save folks like me some head-scratching? 😊

Steps to Reproduce:

Here’s a simple snippet:

import { WsProvider, ApiPromise } from '@polkadot/api';

const provider = new WsProvider('wss://polkadot-rpc.dwellir.com');

async function main() {
  const api = await ApiPromise.create({
    provider,
    noInitWarn: true,
    types: {
      FieldType: {
        _enum: ['Uint8', 'Int8', 'String'],
      },
    },
  });

  // This line throws the error :(
  const fieldTypeEnum = api.createType('FieldType', 'String');
}

main().catch(console.error);

You can also play with it here: StackBlitz Link.

The script is throwing an error.

Error: createType(FieldType):: Cannot map Enum JSON, unable to find 'String' in uint8, int8, text
    at createTypeUnsafe (/home/projects/node-ukhved/node_modules/@polkadot/types-create/cjs/create/type.js:54:22)
    at TypeRegistry.createType (/home/projects/node-ukhved/node_modules/@polkadot/types/cjs/create/registry.js:224:52)
    at ApiPromise.createType (/home/projects/node-ukhved/node_modules/@polkadot/api/cjs/base/Decorate.js:125:42)
    at main (/home/projects/node-ukhved/index.ts:2:469)

Thoughts:

Am I approaching this wrong? Any guidance would be super appreciated—thanks so much for all the amazing work you do! 🙏

@valentinfernandez1
Copy link
Contributor

valentinfernandez1 commented Mar 17, 2025

Ah, I see what's your issue, the type being inferred isn’t actually wrong. The problem is with how the value is being passed in the line that’s throwing the error. You’ll want to do it like this instead:

// The String type is passed as a type WITHOUT quotes.  
const fieldTypeEnum = api.createType("FieldType", String);
console.log(fieldTypeEnum.toRawType());

The output will then be

{"_enum":{"Uint8":0,"Int8":1,"Text":2}}

LMK if that solves your issue 😄

@AtelyPham
Copy link
Author

I'm sorry for the confusion. I need the enum key to be a String, but it's currently being converted to Text, which is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants