UNPKG

7.28 kBTypeScriptView Raw
1// Type definitions for regenerator-runtime 0.13
2// Project: https://github.com/facebook/regenerator
3// Definitions by: ExE Boss <https://github.com/ExE-Boss>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// Minimum TypeScript Version: 3.6
6
7export as namespace regeneratorRuntime;
8declare global {
9 var regeneratorRuntime: typeof import('.');
10}
11
12/**
13 * The implementation of the generator.
14 */
15export type InnerFunction<T = undefined, TYield = unknown, TReturn = unknown, TNext = unknown> = (
16 this: T,
17 context: Context<TYield, TReturn, TNext>,
18) => unknown;
19
20export type ContextLocation = number | 'end';
21export type CompletionType = 'normal' | 'return' | 'throw' | 'break' | 'continue';
22
23// prettier-ignore
24export type TryLocationsList = ReadonlyArray<
25 | readonly [number, number]
26 | readonly [number, number | undefined, number, ContextLocation]
27>;
28
29export interface CompletionRecord {
30 type: CompletionType;
31 arg: unknown;
32}
33
34export interface TryEntry {
35 readonly tryLoc: number;
36 readonly catchLoc?: number | undefined;
37 readonly finallyLoc?: number | undefined;
38 readonly afterLoc?: ContextLocation | undefined;
39 completion?: CompletionRecord | undefined;
40}
41
42export interface DelegatedIterator {
43 iterator: Iterator<unknown, unknown, unknown>;
44}
45
46export interface Context<TYield = unknown, TReturn = unknown, TNext = unknown> {
47 readonly tryEntries: readonly [
48 { readonly tryLoc: 'root' } & Omit<TryEntry, 'tryLoc'>,
49 ...TryEntry[]
50 ];
51
52 /**
53 * The value passed to `next()`.
54 */
55 sent: TNext;
56
57 /**
58 * The label of the previous location, needs to be set to `next` at the start of user code.
59 */
60 prev: unknown;
61
62 /**
63 * The label of the next location, is set to `'end'` when the generator needs to close abruptly.
64 */
65 next: number | 'end';
66
67 /**
68 * Whether the generator has finished.
69 */
70 done: boolean;
71
72 /**
73 * The return value, set by `abrupt("return")`.
74 */
75 rval: TReturn;
76
77 /**
78 * If truthy, then it contains information about the currently `yield*` delegated iterator.
79 */
80 delegate: DelegatedIterator | undefined;
81
82 /**
83 * The generator method.
84 */
85 method: 'next' | 'return' | 'throw';
86
87 /**
88 * The argument passed to the generator method.
89 */
90 arg: unknown;
91
92 reset(skipTempReset?: boolean): void;
93
94 /**
95 * Ends the iteration.
96 */
97 stop(): TReturn;
98
99 /**
100 * Dispatches an exception to `innerFn`
101 *
102 * @param exception The exception to dispatch.
103 */
104 dispatchException(exception: unknown): boolean;
105
106 /**
107 * @param type The completion type.
108 * @param rval The return value.
109 */
110 abrupt(type: 'return', rval?: TReturn): unknown;
111
112 /**
113 * @param type The completion type.
114 * @param exception The exception to throw.
115 */
116 abrupt(type: 'throw', exception?: unknown): never;
117
118 /**
119 * @param type The completion type.
120 * @param nextLoc The location label to resume iteration at.
121 */
122 abrupt(type: 'break' | 'continue', nextLoc: number): unknown;
123
124 /**
125 * @param type The completion type.
126 * @param arg The [[Value]] or [[Target]] of the completion record.
127 */
128 abrupt(type: CompletionType, arg?: unknown): unknown;
129
130 /**
131 * @param record The completion record.
132 * @param afterLoc The location to resume the generator at, only used by normal completions.
133 */
134 complete(record: Readonly<CompletionRecord>, afterLoc?: ContextLocation): unknown;
135
136 /**
137 * Used to signify the end of a finally block.
138 *
139 * @param finallyLoc The label of the beginning of the finally block.
140 */
141 finish(finallyLoc: number): unknown;
142
143 /**
144 * Used to obtain the exception that was thrown in the associated try block.
145 *
146 * @param tryLoc The label of the beginning of the try block.
147 */
148 catch(tryLoc: number): unknown;
149
150 /**
151 * @param iterable The iterable to delegate to.
152 * @param resultName The name of the property to assign to on this context.
153 * @param nextLoc The label of the location where to resume iteration.
154 */
155 delegateYield(
156 iterable: { [Symbol.iterator](): Iterator<TYield, unknown, unknown> },
157 resultName: string,
158 nextLoc: ContextLocation,
159 ): unknown;
160
161 [
162 /**
163 * Expects properties to match `/^t[+-]?\d*(?:(?<=\d)\.\d*|\.\d+)?(?:e[+-]?\d+)?$/`.
164 */
165 temp: string
166 ]: any;
167}
168
169export function wrap<T = undefined, TYield = unknown, TReturn = unknown, TNext = unknown>(
170 innerFn: InnerFunction<T, TYield, TReturn, TNext>,
171 // tslint:disable-next-line: ban-types
172 outerFn?: Function | null,
173 self?: T,
174 tryLocsList?: TryLocationsList,
175): Generator<TYield, TReturn, TNext>;
176
177export interface ResolvablePromiseConstructorLike extends PromiseConstructorLike {
178 resolve<T = void>(value?: T): PromiseLike<T>;
179}
180
181export class AsyncIterator<TYield = unknown, TReturn = unknown, TNext = unknown>
182 implements AsyncGenerator<TYield, TReturn, TNext> {
183 constructor(
184 generator: Generator<
185 TYield | PromiseLike<TYield> | awrap<unknown>,
186 TReturn | PromiseLike<TReturn> | awrap<unknown>
187 >,
188 PromiseImpl: ResolvablePromiseConstructorLike,
189 );
190
191 // NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
192 next(...args: [] | [TNext]): Promise<IteratorResult<TYield, TReturn>>;
193 return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<TYield, TReturn>>;
194 throw(e: any): Promise<IteratorResult<TYield, TReturn>>;
195
196 [Symbol.asyncIterator](): this;
197}
198
199export function async<T = undefined, TYield = unknown, TReturn = unknown>(
200 innerFn: InnerFunction<T, TYield, TReturn>,
201 outerFn: GeneratorFunction,
202 self?: T,
203 tryLocsList?: TryLocationsList,
204 PromiseImpl?: ResolvablePromiseConstructorLike,
205): AsyncIterator<
206 TYield extends PromiseLike<infer Await> ? Await : Exclude<TYield, awrap<unknown>>,
207 TReturn extends PromiseLike<infer Await> ? Await : Exclude<TReturn, awrap<unknown>>
208>;
209export function async<T = undefined, TReturn = unknown>(
210 innerFn: InnerFunction<T, unknown, TReturn>,
211 // tslint:disable-next-line: ban-types
212 outerFn?: Function | null,
213 self?: T,
214 tryLocsList?: TryLocationsList,
215 PromiseImpl?: ResolvablePromiseConstructorLike,
216): Promise<TReturn extends PromiseLike<infer Await> ? Await : TReturn>;
217
218export function awrap<V>(arg: V): awrap<V>;
219export class awrap<V> {
220 constructor(arg: V);
221
222 // Used to tell TypeScript that this class is to be treated as a nominal type:
223 private readonly '#private';
224
225 readonly __await: V;
226}
227
228export function isGeneratorFunction(func: unknown): func is GeneratorFunction;
229
230export function keys(object: {}): () => IteratorResult<string, undefined>;
231
232// tslint:disable-next-line: ban-types
233export function mark<F extends Function>(genFun: F): F & GeneratorFunction;
234
235export function values<I extends Iterator<unknown, unknown, unknown>>(iterable: { [Symbol.iterator](): I }): I;
236export function values<T>(iterableOrArrayLike: Iterable<T> | ArrayLike<T>): Iterator<T, unknown, unknown>;