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

[Question] adding undefined for message types #88

Open
Art3miX opened this issue Dec 5, 2022 · 1 comment
Open

[Question] adding undefined for message types #88

Art3miX opened this issue Dec 5, 2022 · 1 comment

Comments

@Art3miX
Copy link

Art3miX commented Dec 5, 2022

I have an enum:

pub enum MintStatus {
    Pending{},
    Success{token_id: String},
    Error{message: String},
}

This enum is a response to a query in my contract, when I use ts-codegen, it parses it to this:

export type MintStatus = {
  pending: {};
} | {
  success: {
    token_id: string;
  };
} | {
  error: {
    message: string;
  };
};

To me it feels wrong, because this forces me to cast my MintStatus type into any, in order to do something like this:

let mintStatus = query() as any;
if(mintStatus.success) {
  let tokenId = mintStatus.success.token_id;
}

I would suggest to change the parsing into something like this:

export type MintStatus = {
 pending: {} | undefined;
 success: {
   token_id: string;
 } | undefined;
 error: {
   message: string;
 } | undefined;
};

This change will allow me to avoid the any cast, and allow me to get TS work correctly for this type.
Right now it doesn't give me any auto completion and complains when I do not cast to any.

@pyramation
Copy link
Collaborator

I understand why you'd want to make undefined allowed, but then in this case, the MintStatus could be constructed as not proper. I think doing any to construct dynamically is probably the best in terms of data integrity in this case.

Curious about other thoughts and opinions, but I think allowing undefined is probably also not an ideal solution.

@pyramation pyramation changed the title [Question] wrong enum prasing? [Question] adding undefined for message types Dec 8, 2022
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