亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

打字稿。在 tsconfig.json 中使用 ESNext 作為目標時如何防止轉譯?

打字稿。在 tsconfig.json 中使用 ESNext 作為目標時如何防止轉譯?

搖曳的薔薇 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);


查看完整回答
反對 回復 2022-06-16
  • 1 回答
  • 0 關注
  • 303 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號