reactjs - 带有修饰符的 TypeScript 构造函数参数无法使用 ts-loader 正确发出
问题描述
我开始使用使用 TypeScript/React/Storybook 构建的 Web 应用程序。我在写课程时注意到:
class MyClass {
constructor(public a) { }
}
console.log(MyClass);
并使用 jest 运行单元测试,或使用 tsc 发出代码,它会生成预期的 JS 代码,例如:
function MyClass(a) {
this.a = a;
}
但是当我在带有开发服务器的 Storybook 应用程序中运行它时,它会打印出如下 JS 代码:
function MyClass(a) {
_classCallCheck(this, MyClass);
}
我在想它是否与 ts-loader 选择的 TypeScript 编译器版本有关,但无法弄清楚。我检查了安装到我的 node_modules 文件夹中的 TypeScript 版本,它是 v4.1.2,这对我来说看起来没什么问题。
我还在这里提到了剧本(并在标签中留下了“剧本”),只是因为我的应用程序与它在一起。它可能与此问题没有直接关系。
解决方案
@babel/preset-typescript
如果您正在使用它,这似乎是最新版本的问题。您可以更改您的 babel 配置以使用"@babel/plugin-transform-typescript"
插件而不是@babel/preset-typescript
预设。更多信息:https ://www.gitmemory.com/issue/babel/babel/8752/486541662
如果由 storybook 生成,您的 babel 配置可能如下所示:
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
然后将其更改为:
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
plugins: ["@babel/plugin-transform-typescript"],
};
推荐阅读
- python - 如何让 python 应用程序在主机上 24/7 全天候运行?
- python - 将大整数键均匀分布到 N 个桶中
- c++ - 不能在 VS19 中从 'const char*' 转换为 'char*',虽然之前可以
- python - 更改某个角色的频道权限 discord.py
- php - 当前路由的 Laravel 8 前缀
- udp - 使用 netcat 在 localhost 中看不到 tcpreplay 流量
- javascript - 如何通过 onclick 切换开关打开/关闭特定的 JavaScript 脚本?
- html - 为什么我的代码中没有实现 CSS 的特殊性?
- android - 使用子唯一 ID 插入 Firebase 数据
- unity3d - 统一传递精神后如何触发脚本