小编Har*_*vay的帖子

如何使用 SWC 生成 TypeScript 代码

我希望在 Rust 中使用SWC来生成一些 TypeScript 代码。不幸的是,发射器似乎只能打印 JavaScript。这是正确的,还是有办法打印 TypeScript?例如,假设我们正在制作以下 AST。

use swc_atoms::js_word;
use swc_common::{BytePos, Span, SyntaxContext};
use swc_ecmascript;
use swc_ecmascript::ast;

fn main() {
    let program = ast::Program::Module(ast::Module {
        body: vec![ast::ModuleItem::ModuleDecl(ast::ModuleDecl::ExportDecl(
            ast::ExportDecl {
                decl: ast::Decl::TsTypeAlias(ast::TsTypeAliasDecl {
                    span: Span::default(),
                    declare: false,
                    id: ast::Ident::new(js_word!(""), Span::default()),
                    type_params: None,
                    type_ann: Box::new(ast::TsType::TsKeywordType(ast::TsKeywordType {
                        span: Span::default(),
                        kind: ast::TsKeywordTypeKind::TsStringKeyword,
                    })),
                }),
                span: Span::default(),
            },
        ))],
        shebang: None,
        span: Span::new(BytePos(1), BytePos(1), SyntaxContext::empty()),
    });
}
Run Code Online (Sandbox Code Playgroud)

如何获取与programAST 对应的 TypeScript?

abstract-syntax-tree rust codegen typescript swc-compiler

9
推荐指数
1
解决办法
1917
查看次数