【发布时间】:2023-03-18 06:34:01
【问题描述】:
在使用@babel/parser解析一些字符串并得到一个ast后,我在按obj.prop类型记录ast时遇到错误,但在使用obj['prop']类型时它可以工作
import { parse } from "@babel/parser";
import traverse from "@babel/traverse";
const ast = parse('{key: "something"}', {
sourceType: "module",
plugins: ["typescript"],
});
// Property 'declaration' does not exist on type 'Statement'. Property 'declaration' does not exist on type 'BlockStatement'.ts(2339)
console.log(ast.program.body[0].declaration.properties);
// it's good
console.log(ast.program.body[0]["declaration"].properties);
我对这两种写法的区别感到困惑?
感谢回答
【问题讨论】:
标签: typescript object babeljs