index.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { BaseError } from 'make-error';
  2. import type * as _ts from 'typescript';
  3. import type { TSCommon } from './ts-compiler-types';
  4. import type { createEsmHooks as createEsmHooksFn } from './esm';
  5. export { TSCommon };
  6. export { createRepl, CreateReplOptions, ReplService, EvalAwarePartialHost, } from './repl';
  7. export type { TranspilerModule, TranspilerFactory, CreateTranspilerOptions, TranspileOutput, TranspileOptions, Transpiler, } from './transpilers/types';
  8. export type { NodeLoaderHooksAPI1, NodeLoaderHooksAPI2, NodeLoaderHooksFormat, } from './esm';
  9. /**
  10. * Registered `ts-node` instance information.
  11. */
  12. export declare const REGISTER_INSTANCE: unique symbol;
  13. /**
  14. * Expose `REGISTER_INSTANCE` information on node.js `process`.
  15. */
  16. declare global {
  17. namespace NodeJS {
  18. interface Process {
  19. [REGISTER_INSTANCE]?: Service;
  20. }
  21. }
  22. }
  23. /**
  24. * Export the current version.
  25. */
  26. export declare const VERSION: any;
  27. /**
  28. * Options for creating a new TypeScript compiler instance.
  29. * @category Basic
  30. */
  31. export interface CreateOptions {
  32. /**
  33. * Behave as if invoked within this working directory. Roughly equivalent to `cd $dir && ts-node ...`
  34. *
  35. * @default process.cwd()
  36. */
  37. cwd?: string;
  38. /**
  39. * Legacy alias for `cwd`
  40. *
  41. * @deprecated use `projectSearchDir` or `cwd`
  42. */
  43. dir?: string;
  44. /**
  45. * Emit output files into `.ts-node` directory.
  46. *
  47. * @default false
  48. */
  49. emit?: boolean;
  50. /**
  51. * Scope compiler to files within `scopeDir`.
  52. *
  53. * @default false
  54. */
  55. scope?: boolean;
  56. /**
  57. * @default First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.
  58. */
  59. scopeDir?: string;
  60. /**
  61. * Use pretty diagnostic formatter.
  62. *
  63. * @default false
  64. */
  65. pretty?: boolean;
  66. /**
  67. * Use TypeScript's faster `transpileModule`.
  68. *
  69. * @default false
  70. */
  71. transpileOnly?: boolean;
  72. /**
  73. * **DEPRECATED** Specify type-check is enabled (e.g. `transpileOnly == false`).
  74. *
  75. * @default true
  76. */
  77. typeCheck?: boolean;
  78. /**
  79. * Use TypeScript's compiler host API instead of the language service API.
  80. *
  81. * @default false
  82. */
  83. compilerHost?: boolean;
  84. /**
  85. * Logs TypeScript errors to stderr instead of throwing exceptions.
  86. *
  87. * @default false
  88. */
  89. logError?: boolean;
  90. /**
  91. * Load "files" and "include" from `tsconfig.json` on startup.
  92. *
  93. * Default is to override `tsconfig.json` "files" and "include" to only include the entrypoint script.
  94. *
  95. * @default false
  96. */
  97. files?: boolean;
  98. /**
  99. * Specify a custom TypeScript compiler.
  100. *
  101. * @default "typescript"
  102. */
  103. compiler?: string;
  104. /**
  105. * Specify a custom transpiler for use with transpileOnly
  106. */
  107. transpiler?: string | [string, object];
  108. /**
  109. * Transpile with swc instead of the TypeScript compiler, and skip typechecking.
  110. *
  111. * Equivalent to setting both `transpileOnly: true` and `transpiler: 'ts-node/transpilers/swc'`
  112. *
  113. * For complete instructions: https://typestrong.org/ts-node/docs/transpilers
  114. */
  115. swc?: boolean;
  116. /**
  117. * Paths which should not be compiled.
  118. *
  119. * Each string in the array is converted to a regular expression via `new RegExp()` and tested against source paths prior to compilation.
  120. *
  121. * Source paths are normalized to posix-style separators, relative to the directory containing `tsconfig.json` or to cwd if no `tsconfig.json` is loaded.
  122. *
  123. * Default is to ignore all node_modules subdirectories.
  124. *
  125. * @default ["(?:^|/)node_modules/"]
  126. */
  127. ignore?: string[];
  128. /**
  129. * Path to TypeScript config file or directory containing a `tsconfig.json`.
  130. * Similar to the `tsc --project` flag: https://www.typescriptlang.org/docs/handbook/compiler-options.html
  131. */
  132. project?: string;
  133. /**
  134. * Search for TypeScript config file (`tsconfig.json`) in this or parent directories.
  135. */
  136. projectSearchDir?: string;
  137. /**
  138. * Skip project config resolution and loading.
  139. *
  140. * @default false
  141. */
  142. skipProject?: boolean;
  143. /**
  144. * Skip ignore check, so that compilation will be attempted for all files with matching extensions.
  145. *
  146. * @default false
  147. */
  148. skipIgnore?: boolean;
  149. /**
  150. * JSON object to merge with TypeScript `compilerOptions`.
  151. *
  152. * @allOf [{"$ref": "https://schemastore.azurewebsites.net/schemas/json/tsconfig.json#definitions/compilerOptionsDefinition/properties/compilerOptions"}]
  153. */
  154. compilerOptions?: object;
  155. /**
  156. * Ignore TypeScript warnings by diagnostic code.
  157. */
  158. ignoreDiagnostics?: Array<number | string>;
  159. /**
  160. * Modules to require, like node's `--require` flag.
  161. *
  162. * If specified in `tsconfig.json`, the modules will be resolved relative to the `tsconfig.json` file.
  163. *
  164. * If specified programmatically, each input string should be pre-resolved to an absolute path for
  165. * best results.
  166. */
  167. require?: Array<string>;
  168. readFile?: (path: string) => string | undefined;
  169. fileExists?: (path: string) => boolean;
  170. transformers?: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers);
  171. /**
  172. * Allows the usage of top level await in REPL.
  173. *
  174. * Uses node's implementation which accomplishes this with an AST syntax transformation.
  175. *
  176. * Enabled by default when tsconfig target is es2018 or above. Set to false to disable.
  177. *
  178. * **Note**: setting to `true` when tsconfig target is too low will throw an Error. Leave as `undefined`
  179. * to get default, automatic behavior.
  180. */
  181. experimentalReplAwait?: boolean;
  182. /**
  183. * Override certain paths to be compiled and executed as CommonJS or ECMAScript modules.
  184. * When overridden, the tsconfig "module" and package.json "type" fields are overridden, and
  185. * the file extension is ignored.
  186. * This is useful if you cannot use .mts, .cts, .mjs, or .cjs file extensions;
  187. * it achieves the same effect.
  188. *
  189. * Each key is a glob pattern following the same rules as tsconfig's "include" array.
  190. * When multiple patterns match the same file, the last pattern takes precedence.
  191. *
  192. * `cjs` overrides matches files to compile and execute as CommonJS.
  193. * `esm` overrides matches files to compile and execute as native ECMAScript modules.
  194. * `package` overrides either of the above to default behavior, which obeys package.json "type" and
  195. * tsconfig.json "module" options.
  196. */
  197. moduleTypes?: ModuleTypes;
  198. /**
  199. * A function to collect trace messages from the TypeScript compiler, for example when `traceResolution` is enabled.
  200. *
  201. * @default console.log
  202. */
  203. tsTrace?: (str: string) => void;
  204. /**
  205. * Enable native ESM support.
  206. *
  207. * For details, see https://typestrong.org/ts-node/docs/imports#native-ecmascript-modules
  208. */
  209. esm?: boolean;
  210. /**
  211. * Re-order file extensions so that TypeScript imports are preferred.
  212. *
  213. * For example, when both `index.js` and `index.ts` exist, enabling this option causes `require('./index')` to resolve to `index.ts` instead of `index.js`
  214. *
  215. * @default false
  216. */
  217. preferTsExts?: boolean;
  218. /**
  219. * Like node's `--experimental-specifier-resolution`, , but can also be set in your `tsconfig.json` for convenience.
  220. *
  221. * For details, see https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm
  222. */
  223. experimentalSpecifierResolution?: 'node' | 'explicit';
  224. /**
  225. * Allow using voluntary `.ts` file extension in import specifiers.
  226. *
  227. * Typically, in ESM projects, import specifiers must have an emit extension, `.js`, `.cjs`, or `.mjs`,
  228. * and we automatically map to the corresponding `.ts`, `.cts`, or `.mts` source file. This is the
  229. * recommended approach.
  230. *
  231. * However, if you really want to use `.ts` in import specifiers, and are aware that this may
  232. * break tooling, you can enable this flag.
  233. */
  234. experimentalTsImportSpecifiers?: boolean;
  235. }
  236. export declare type ModuleTypes = Record<string, ModuleTypeOverride>;
  237. export declare type ModuleTypeOverride = 'cjs' | 'esm' | 'package';
  238. /**
  239. * Options for registering a TypeScript compiler instance globally.
  240. * @category Basic
  241. */
  242. export interface RegisterOptions extends CreateOptions {
  243. /**
  244. * Enable experimental features that re-map imports and require calls to support:
  245. * `baseUrl`, `paths`, `rootDirs`, `.js` to `.ts` file extension mappings,
  246. * `outDir` to `rootDir` mappings for composite projects and monorepos.
  247. *
  248. * For details, see https://github.com/TypeStrong/ts-node/issues/1514
  249. */
  250. experimentalResolver?: boolean;
  251. }
  252. export declare type ExperimentalSpecifierResolution = 'node' | 'explicit';
  253. /**
  254. * Must be an interface to support `typescript-json-schema`.
  255. */
  256. export interface TsConfigOptions extends Omit<RegisterOptions, 'transformers' | 'readFile' | 'fileExists' | 'skipProject' | 'project' | 'dir' | 'cwd' | 'projectSearchDir' | 'optionBasePaths' | 'tsTrace'> {
  257. }
  258. /**
  259. * Information retrieved from type info check.
  260. */
  261. export interface TypeInfo {
  262. name: string;
  263. comment: string;
  264. }
  265. /**
  266. * TypeScript diagnostics error.
  267. */
  268. export declare class TSError extends BaseError {
  269. diagnosticCodes: number[];
  270. name: string;
  271. diagnosticText: string;
  272. diagnostics: ReadonlyArray<_ts.Diagnostic>;
  273. constructor(diagnosticText: string, diagnosticCodes: number[], diagnostics?: ReadonlyArray<_ts.Diagnostic>);
  274. }
  275. /**
  276. * Primary ts-node service, which wraps the TypeScript API and can compile TypeScript to JavaScript
  277. */
  278. export interface Service {
  279. ts: TSCommon;
  280. config: _ts.ParsedCommandLine;
  281. options: RegisterOptions;
  282. enabled(enabled?: boolean): boolean;
  283. ignored(fileName: string): boolean;
  284. compile(code: string, fileName: string, lineOffset?: number): string;
  285. getTypeInfo(code: string, fileName: string, position: number): TypeInfo;
  286. }
  287. /**
  288. * Re-export of `Service` interface for backwards-compatibility
  289. * @deprecated use `Service` instead
  290. * @see {Service}
  291. */
  292. export declare type Register = Service;
  293. /**
  294. * Create a new TypeScript compiler instance and register it onto node.js
  295. *
  296. * @category Basic
  297. */
  298. export declare function register(opts?: RegisterOptions): Service;
  299. /**
  300. * Register TypeScript compiler instance onto node.js
  301. * @category Basic
  302. */
  303. export declare function register(service: Service): Service;
  304. /**
  305. * Create TypeScript compiler instance.
  306. *
  307. * @category Basic
  308. */
  309. export declare function create(rawOptions?: CreateOptions): Service;
  310. /**
  311. * Create an implementation of node's ESM loader hooks.
  312. *
  313. * This may be useful if you
  314. * want to wrap or compose the loader hooks to add additional functionality or
  315. * combine with another loader.
  316. *
  317. * Node changed the hooks API, so there are two possible APIs. This function
  318. * detects your node version and returns the appropriate API.
  319. *
  320. * @category ESM Loader
  321. */
  322. export declare const createEsmHooks: typeof createEsmHooksFn;
  323. /**
  324. * When using `module: nodenext` or `module: node12`, there are two possible styles of emit depending in file extension or package.json "type":
  325. *
  326. * - CommonJS with dynamic imports preserved (not transformed into `require()` calls)
  327. * - ECMAScript modules with `import foo = require()` transformed into `require = createRequire(); const foo = require()`
  328. */
  329. export declare type NodeModuleEmitKind = 'nodeesm' | 'nodecjs';