马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * A utility type for choosing between synchronous and asynchronous return
 * values.
 *
 * This is used as the return value for plugins like [[CustomFunction]],
 * [[Importer]], and [[FileImporter]] so that TypeScript enforces that
 * asynchronous plugins are only passed to [[compileAsync]] and
 * [[compileStringAsync]], not [[compile]] or [[compileString]].
 *
 * @typeParam sync - If this is `'sync'`, this can only be a `T`. If it's
 * `'async'`, this can be either a `T` or a `Promise<T>`.
 *
 * @category Other
 */
export type PromiseOr<T, sync extends 'sync' | 'async'> = sync extends 'async'
  ? T | Promise<T>
  : T;