472 - 《ts-black-space》
发布于 2024年10月12日
翻了下 ts-blank-space 的源码,做下笔记,附带一个 toy version。
1、ts-blank-space 的功能是移除 TypeScript 代码中的类型注解,基于 typescript 的 transform api,没有用 wasm,没有用 native 语言,纯 js 实现,但性能只相比 native + async 稍慢一点点。
2、ts-blank-space 的实现原理是基于 typescript 的 transform api,遍历并更新所有变量声明节点,移除其类型注解。
3、例行提取其中最简逻辑,写个 Toy Version 如下。
import * as ts from "typescript";
function removeTypeAnnotation(input) {
const sourceFile = ts.createSourceFile(
"input.ts",
input,
ts.ScriptTarget.Latest,
true
);
function visit(node) {
if (ts.isVariableDeclaration(node) && node.type) {