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

Closed
AtelyPham opened this issue Mar 14, 2025 · 7 comments
Closed

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

AtelyPham opened this issue Mar 14, 2025 · 7 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.

@valentinfernandez1
Copy link
Contributor

Can you provide more context as to why this is causing issues🙏🏻.

AFAIK the Text type should be compatible with any of the strings that you need to submit with the added benefit that it's compatible with SCALE encoding, that is the reason why a String is converted to Text.

@AtelyPham
Copy link
Author

I understand your point, but I need to know how to use a String as the key of an enum. Essentially, how do I create an enum where the key is a String, not its value type?

@valentinfernandez1
Copy link
Contributor

The reason you can’t use String directly is actually by design. Since the API needs types that can be SCALE encoded, String gets normalized to Text to match what Substrate expects.

We could definitely add some comments or docs to make this clearer.

That said, Text works fine with regular JavaScript String inputs. Just to help us understand better, is this mainly about wanting the enum key to literally say String, or is the remapping to Text actually causing issues in your code? If so please provide logs of the issue so we can craft a reasonable fix

@AtelyPham
Copy link
Author

Yes, I need the enum key to literally be String, not remapped to Text.

@valentinfernandez1
Copy link
Contributor

Since this behavior is intentional and part of how the API aligns with Substrate types, I’ll go ahead and close the issue. If you run into a concrete issue where this impacts functionality beyond naming, feel free to reopen or open a new issue with more details.

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