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

Mirror JCO generated Result type #8

Open
nicoburniske opened this issue Aug 27, 2024 · 0 comments
Open

Mirror JCO generated Result type #8

nicoburniske opened this issue Aug 27, 2024 · 0 comments

Comments

@nicoburniske
Copy link
Member

/** Type representing success or failure. */
export type Result<T, E = unknown> = Result.Ok<T> | Result.Err<E>;

export namespace Result {
    /**
     * The type of a successful result.
     * @example
     * const success: Result.ok<number> = Result.ok(123)
     */
    export type Ok<T> = typeof prototype & {
        readonly value: T;
        readonly error?: never;
        readonly isOk: true;
        readonly isErr: false;
    };

    /**
     * A failed result.
     * @example
     * const failure: Result.err<string> = Result.err('error')
     */
    export type Err<E> = typeof prototype & {
        readonly value?: never;
        readonly error: E;
        readonly isOk: false;
        readonly isErr: true;
    };

value field should always contain the success value, or the error

there should be a field "tag" that is either "ok" or "err" like the type generated by JCO

See here https://github.com/golemcloud/jco/blob/085e0b32eaa91e9b2e1783dc56515f61fc62583f/crates/js-component-bindgen/src/ts_stubgen.rs#L498

export type Result<T, E> = { tag: "ok"; val: T } | { tag: "err"; val: E }

This would make the two result types more consistent. We should also include conversion types between the two if possible

Also need to test if once the change is good, you can just return a golem-ts Result in your wit world exported functions, instead of converting back and forth. would be great if we can support this

@nicoburniske nicoburniske changed the title We should mirror JCO generated Result type Mirror JCO generated Result type Aug 27, 2024
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