Coder Social home page Coder Social logo

Comments (1)

RyanCavanaugh avatar RyanCavanaugh commented on April 28, 2024 1

Slightly reduced (could go farther probably) + annotated

interface Result<T, E> {
  [Symbol.iterator](): Generator<E, T>
}

type NotResultOf<T> = T extends Result<any, any> ? never : T;
type ErrTypeOf<T> = T extends Result<unknown, infer E> ? E : never;
type OkTypeOf<T> = T extends Result<infer R, unknown> ? R : never;
type AsyncJob<T, E> = () => AsyncGenerator<E, T>;

declare const asyncDo: <T, E>(job: AsyncJob<T, E>) => Promise<Result<
  OkTypeOf<T> | NotResultOf<T>,
  E | ErrTypeOf<T>
>>;
declare const mapErr: <E, F>(mapper: (error: E) => F) => <T>(result: Result<T, E>) => Result<T, F>;

type Book = { id: string; title: string; authorId: string };
type Author = { id: string; name: string };
type BookWithAuthor = Book & { author: Author };

declare const fetchAuthor: (id: string) => Promise<Result<Author, "NOT_FOUND">>;

let authorId: string = "";

const f1 = asyncDo(async function* () {
  const author: Author = yield* await fetchAuthor(authorId)

  return null! as BookWithAuthor;
});

const f2: Promise<Result<BookWithAuthor, "NOT_FOUND_BOOK" | "NOT_FOUND_AUTHOR">> = asyncDo(async function* () {
  
  // Without yield*, the type of test1 is
  //   Result<Author, "NOT_FOUND_AUTHOR>
  const test1 = await fetchAuthor(authorId).then(mapErr(() => "NOT_FOUND_AUTHOR" as const))
  //     ^?

  // With yield*, the type of test2 is
  //    Author | BookWithAuthor
  // But this codepath has no way to produce BookWithAuthor
  const test2 = yield* await fetchAuthor(authorId).then(mapErr(() => "NOT_FOUND_AUTHOR" as const))
  //    ^?

  return null! as BookWithAuthor;
});

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.