UNPKG

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