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

Enum schema failed with typescript check when (de)serialize #79

Open
kayac-chang opened this issue Nov 28, 2023 · 2 comments
Open

Enum schema failed with typescript check when (de)serialize #79

kayac-chang opened this issue Nov 28, 2023 · 2 comments

Comments

@kayac-chang
Copy link

Depending on the documentation, the enum schema should be

{ enum: [ { struct: { className1: structSchema1 } }, { struct: { className2: structSchema2 } }, ... ] }

Suppose we have a schema like

const borsh_schema = {
  enum: [
    { 
      struct: { 
        transfer: { 
          struct: {
            recipient: 'string',
            amount: "u64",
          },
        }
      }
    },
    { 
      struct: { 
        deploy: {
          struct: {
            contract: { array: { type: "u8" } },
            cbi_version: "u32",
          },
        }
      } 
    },
  ],
}

The type of schema will become (this is intended behavior)

const borsh_schema: {
    enum: ({
        struct: {
            transfer: {
                struct: {
                    recipient: string;
                    amount: string;
                };
            };
            deploy?: undefined;
        };
    } | {
        struct: {
            deploy: {
                struct: {
                    contract: {
                        array: {
                            type: string;
                        };
                    };
                    cbi_version: string;
                };
            };
            transfer?: undefined;
        };
    })[];
}

But this will fail the type check when we want to pass schema to (de)serialize

deserialize(borsh_schema, bytes)
// this will failed the type check

Typescript Playground: https://tsplay.dev/WPM05w

@hanakannzashi
Copy link

Enum works for me. You may be interested in borsher

@gagdiez
Copy link
Contributor

gagdiez commented Jan 30, 2024

@kayac-chang I did not understand the issue, could you please share a repo with the error so I can reproduce it and check on it?

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

3 participants