搖曳的薔薇
2022-06-16 16:40:24
我已經找到了這個問題并相信它應該有所幫助:How to keep ES6 syntax when transpiling with Typescript,但沒有任何運氣......它有點不同。就我而言,雖然yarn tsc --project tsconfig.json文件包含:// ./index.tsxclass Person { public name: string; constructor(name: string) { this.name = name; } _run = () => { console.log('I\'m running!') }}let person = new Person('John Doe');console.log(person.name);變成了:// ./index.jsclass Person { constructor(name) { this._run = () => { console.log('I\'m running!'); }; this.name = name; }}let person = new Person('John Doe');console.log(person.name);最終,我怎樣才能獲得與輸入時相同的代碼?例如,沒有任何后處理。我的 tsconfig.json:{ "compilerOptions": { "baseUrl": ".", "alwaysStrict": true, "noImplicitAny": false, "noUnusedLocals": true, "noUnusedParameters": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "allowJs": true, "checkJs": false, "module": "ESNext", "target": "ESNext", "jsx": "react", "moduleResolution": "node", "types": ["node"], "lib": ["dom", "es6", "es2017", "es2018", "es2019", "es2020","esnext"] }, "linebreak-style": [true, "LF"], "typeAcquisition": { "enable": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "**/*.test.ts", "**/*.test.tsx", "dist" ]}
1 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
啟用useDefineForClassFieldsintsconfig.json將生成更類似于您的 TypeScript 源的 JavaScript 代碼:
{
"compilerOptions": {
"useDefineForClassFields": true
}
}
使用您的示例:
// ./index.tsx
class Person {
public name: string;
constructor(name: string) {
this.name = name;
}
_run = () => {
console.log('I\'m running!')
}
}
let person = new Person('John Doe');
console.log(person.name);
將被轉譯為:
// index.js
"use strict";
class Person {
name;
constructor(name) {
this.name = name;
}
_run = () => {
console.log('I\'m running!');
};
}
let person = new Person('John Doe');
console.log(person.name);
添加回答
舉報
0/150
提交
取消