module.stripTypeScriptTypes(code[, options])


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active development

  • code <string> 要从中剥离类型注释的代码。

    ¥code <string> The code to strip type annotations from.

  • options <Object>

    • mode <string> 默认值:'strip'。可能的值是:

      ¥mode <string> Default: 'strip'. Possible values are:

      • 'strip' 仅剥离类型注释,而不执行 TypeScript 特性的转换。

        ¥'strip' Only strip type annotations without performing the transformation of TypeScript features.

      • 'transform' 剥离类型注释并将 TypeScript 功能转换为 JavaScript。

        ¥'transform' Strip type annotations and transform TypeScript features to JavaScript.

    • sourceMap <boolean> 默认值:false。仅当 mode'transform'(如果为 true)时,才会为转换后的代码生成源映射。

      ¥sourceMap <boolean> Default: false. Only when mode is 'transform', if true, a source map will be generated for the transformed code.

    • sourceUrl <string> 指定源映射中使用的源 URL。

      ¥sourceUrl <string> Specifies the source url used in the source map.

  • 返回:<string> 剥离类型注释的代码。module.stripTypeScriptTypes() 从 TypeScript 代码中删除类型注释。它可用于在使用 vm.runInContext()vm.compileFunction() 运行 TypeScript 代码之前从中剥离类型注释。默认情况下,如果代码包含需要转换的 TypeScript 功能(例如 Enums),它将抛出错误,有关更多信息,请参阅 type-stripping。当模式为 'transform' 时,它还会将 TypeScript 功能转换为 JavaScript,有关更多信息,请参阅 转换 TypeScript 功能。当模式为 'strip' 时,不会生成源映射,因为位置被保留了。如果提供了 sourceMap,当模式为 'strip' 时,将引发错误。

    ¥Returns: <string> The code with type annotations stripped. module.stripTypeScriptTypes() removes type annotations from TypeScript code. It can be used to strip type annotations from TypeScript code before running it with vm.runInContext() or vm.compileFunction(). By default, it will throw an error if the code contains TypeScript features that require transformation such as Enums, see type-stripping for more information. When mode is 'transform', it also transforms TypeScript features to JavaScript, see transform TypeScript features for more information. When mode is 'strip', source maps are not generated, because locations are preserved. If sourceMap is provided, when mode is 'strip', an error will be thrown.

WARNING:由于 TypeScript 解析器的变化,此函数的输出不应被视为跨 Node.js 版本的稳定输出。

¥WARNING: The output of this function should not be considered stable across Node.js versions, due to changes in the TypeScript parser.

import { stripTypeScriptTypes } from 'node:module';
const code = 'const a: number = 1;';
const strippedCode = stripTypeScriptTypes(code);
console.log(strippedCode);
// Prints: const a         = 1;const { stripTypeScriptTypes } = require('node:module');
const code = 'const a: number = 1;';
const strippedCode = stripTypeScriptTypes(code);
console.log(strippedCode);
// Prints: const a         = 1;

如果提供了 sourceUrl,它将被用作输出末尾的注释:

¥If sourceUrl is provided, it will be used appended as a comment at the end of the output:

import { stripTypeScriptTypes } from 'node:module';
const code = 'const a: number = 1;';
const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' });
console.log(strippedCode);
// Prints: const a         = 1\n\n//# sourceURL=source.ts;const { stripTypeScriptTypes } = require('node:module');
const code = 'const a: number = 1;';
const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' });
console.log(strippedCode);
// Prints: const a         = 1\n\n//# sourceURL=source.ts;

mode'transform' 时,代码将转换为 JavaScript:

¥When mode is 'transform', the code is transformed to JavaScript:

import { stripTypeScriptTypes } from 'node:module';
const code = `
  namespace MathUtil {
    export const add = (a: number, b: number) => a + b;
  }`;
const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
console.log(strippedCode);
// Prints:
// var MathUtil;
// (function(MathUtil) {
//     MathUtil.add = (a, b)=>a + b;
// })(MathUtil || (MathUtil = {}));
// # sourceMappingURL=data:application/json;base64, ...const { stripTypeScriptTypes } = require('node:module');
const code = `
  namespace MathUtil {
    export const add = (a: number, b: number) => a + b;
  }`;
const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
console.log(strippedCode);
// Prints:
// var MathUtil;
// (function(MathUtil) {
//     MathUtil.add = (a, b)=>a + b;
// })(MathUtil || (MathUtil = {}));
// # sourceMappingURL=data:application/json;base64, ...