module.stripTypeScriptTypes(code[, options])
module.stripTypeScriptTypes() 可以从 TypeScript 代码中移除类型注解。它可以在使用 vm.runInContext() 或 vm.compileFunction() 运行 TypeScript 代码之前,用于去除类型注解。
默认情况下,如果代码包含需要转换的 TypeScript 特性(例如 Enums),它将抛出错误,更多信息请参见 类型剥离。
🌐 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.
当模式为 'transform' 时,它还会将 TypeScript 特性转换为 JavaScript,更多信息请参见 转换 TypeScript 功能。
🌐 When mode is 'transform', it also transforms TypeScript features to JavaScript,
see transform TypeScript features for more information.
当模式为 'strip' 时,不会生成源映射,因为会保留位置信息。
如果提供了 sourceMap,当模式为 'strip' 时,将会抛出错误。
🌐 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.
警告: 由于 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, ...