module: add module.stripTypeScriptTypes · nodejs/node@6575b76
@@ -270,6 +270,105 @@ changes:
270270Register a module that exports [hooks][] that customize Node.js module
271271resolution and loading behavior. See [Customization hooks][].
272272273+## `module.stripTypeScriptTypes(code[, options])`
274+275+<!-- YAML
276+added: REPLACEME
277+-->
278+279+> Stability: 1.0 - Early development
280+281+* `code` {string} The code to strip type annotations from.
282+* `options` {Object}
283+ * `mode` {string} **Default:** `'strip'`. Possible values are:
284+ * `'strip'` Only strip type annotations without performing the transformation of TypeScript features.
285+ * `'transform'` Strip type annotations and transform TypeScript features to JavaScript.
286+ * `sourceMap` {boolean} **Default:** `false`. Only when `mode` is `'transform'`, if `true`, a source map
287+ will be generated for the transformed code.
288+ * `sourceUrl` {string} Specifies the source url used in the source map.
289+* Returns: {string} The code with type annotations stripped.
290+ `module.stripTypeScriptTypes()` removes type annotations from TypeScript code. It
291+ can be used to strip type annotations from TypeScript code before running it
292+ with `vm.runInContext()` or `vm.compileFunction()`.
293+ By default, it will throw an error if the code contains TypeScript features
294+ that require transformation such as `Enums`,
295+ see [type-stripping][] for more information.
296+ When mode is `'transform'`, it also transforms TypeScript features to JavaScript,
297+ see [transform TypeScript features][] for more information.
298+ When mode is `'strip'`, source maps are not generated, because locations are preserved.
299+ If `sourceMap` is provided, when mode is `'strip'`, an error will be thrown.
300+301+_WARNING_: The output of this function should not be considered stable across Node.js versions,
302+due to changes in the TypeScript parser.
303+304+```mjs
305+import { stripTypeScriptTypes } from 'node:module';
306+const code = 'const a: number = 1;';
307+const strippedCode = stripTypeScriptTypes(code);
308+console.log(strippedCode);
309+// Prints: const a = 1;
310+```
311+312+```cjs
313+const { stripTypeScriptTypes } = require('node:module');
314+const code = 'const a: number = 1;';
315+const strippedCode = stripTypeScriptTypes(code);
316+console.log(strippedCode);
317+// Prints: const a = 1;
318+```
319+320+If `sourceUrl` is provided, it will be used appended as a comment at the end of the output:
321+322+```mjs
323+import { stripTypeScriptTypes } from 'node:module';
324+const code = 'const a: number = 1;';
325+const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' });
326+console.log(strippedCode);
327+// Prints: const a = 1\n\n//# sourceURL=source.ts;
328+```
329+330+```cjs
331+const { stripTypeScriptTypes } = require('node:module');
332+const code = 'const a: number = 1;';
333+const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' });
334+console.log(strippedCode);
335+// Prints: const a = 1\n\n//# sourceURL=source.ts;
336+```
337+338+When `mode` is `'transform'`, the code is transformed to JavaScript:
339+340+```mjs
341+import { stripTypeScriptTypes } from 'node:module';
342+const code = `
343+ namespace MathUtil {
344+ export const add = (a: number, b: number) => a + b;
345+ }`;
346+const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
347+console.log(strippedCode);
348+// Prints:
349+// var MathUtil;
350+// (function(MathUtil) {
351+// MathUtil.add = (a, b)=>a + b;
352+// })(MathUtil || (MathUtil = {}));
353+// # sourceMappingURL=data:application/json;base64, ...
354+```
355+356+```cjs
357+const { stripTypeScriptTypes } = require('node:module');
358+const code = `
359+ namespace MathUtil {
360+ export const add = (a: number, b: number) => a + b;
361+ }`;
362+const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
363+console.log(strippedCode);
364+// Prints:
365+// var MathUtil;
366+// (function(MathUtil) {
367+// MathUtil.add = (a, b)=>a + b;
368+// })(MathUtil || (MathUtil = {}));
369+// # sourceMappingURL=data:application/json;base64, ...
370+```
371+273372### `module.syncBuiltinESMExports()`
274373275374<!-- YAML
@@ -1251,3 +1350,5 @@ returned object contains the following keys:
12511350[realm]: https://tc39.es/ecma262/#realm
12521351[source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx
12531352[transferrable objects]: worker_threads.md#portpostmessagevalue-transferlist
1353+[transform TypeScript features]: typescript.md#typescript-features
1354+[type-stripping]: typescript.md#type-stripping