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

Type annotations improvement #356

Open
garyhe0780 opened this issue May 11, 2024 · 0 comments
Open

Type annotations improvement #356

garyhe0780 opened this issue May 11, 2024 · 0 comments

Comments

@garyhe0780
Copy link

garyhe0780 commented May 11, 2024

const { data, error } = await resend.emails.send({
    from: 'Acme <[email protected]>',
    to: ['[email protected]'],
    subject: 'Hello World',
    html: '<strong>It works!</strong>',
});

if (error) {
    return console.error({ error });
}

console.log({ id: data.id });

taking an example from https://resend.com/docs/send-with-nodejs, If you access id from data directly, ts will complain that 'data' is possibly 'null', and that's because of return type:

 async fetchRequest<T>(
    path: string,
    options = {},
  ): Promise<{ data: T | null; error: ErrorResponse | null }>

and I think it's not accurate because from the code we can know that data can not be null when error is null, and it can be changed like this:

export type SuccessResponse<T> = {
  data: T;
  error: null;
};

export type FailureResponse = {
  error: ErrorResponse
  data: null;
}

export type Result<T> = SuccessResponse<T> | FailureResponse;
async fetchRequest<T>(
    path: string,
    options = {},
 ): Promise<Result<T>>
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

1 participant