Comments (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)
- `tsc` compile `proposal-explicit-resource-management` different from `babel` and `swc` HOT 1
- After enabling noUncheckedIndexedAccess, using ++ on a default undefined does not trigger an error message. HOT 3
- C++-style `const` modifier on class members HOT 2
- Package Import is not working from neither way "CommonJS" or "Module" HOT 4
- Type guard for child not transitive parent object HOT 3
- Error when compiled JavaScript initializes static properties in a class with a hard private method (`#`) that references a static property
- The ?: inference failed; no selection was made between ifTrue and ifFalse. HOT 1
- Add Support for Flow's new component syntax HOT 6
- Documentation: "Creating and Printing a TypeScript AST" example does not work after v4.9.5
- `satisfies` does not work on a variable called `type`
- API: Can't seem to extract JsDoc tags from declaration HOT 2
- Add option to detect and strip internal exports
- false error on generic type alias parameters HOT 5
- Unable to declare an interface that extends `Record<string, nonAnyType>` with additional support functions. HOT 7
- Add support for generic types on a index accessor HOT 2
- `getTextOfJSDocComment` is stripping `#` from JSDoc comment HOT 1
- Enable `allowJs` with `isolatedDeclarations` HOT 1
- JSDoc comment string with the keyword "@private" before import statement in JS file result in cryptic error TS1191 during compilation HOT 2
- `NoInfer` isn't erased and breaks type narrowing HOT 3
- function with overloading incorrectly defines generic variables HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.