tsconfg中的target,module和moduleResolution
target
就是TypeScript文件编译后生成的javascript文件里的语法应该遵循哪个JavaScript的版本。可选项为:"ES5" , "ES6" / "ES2015" , "ES2016" , "ES2017" 或 "ESNext"
module
就是你的TypeScript文件中的module,采用何种方式实现,可选项为:"None" , "CommonJS" , "AMD" , "System" , "UMD" , "ES6" 或 "ES2015"。具体每一个module的定义,请参考链接:
https://medium.com/computed-comparisons/commonjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b
moduleResolution
就是告诉TypeScript编译器,采用何种方式解析(也就是查找)TypeScript文件中依赖的模块的位置,可选项为:Classic和Node,具体定义,请参考链接:
https://www.tslang.cn/docs/handbook/module-resolution.html
target举例
ts文件中的源代码
1 async function helloWorld(): Promise<string> {
2 const res = await fetch('/static/data.json');
3 const txt = await res.text();
4 return txt;
5 }
6 (async ()=>{
7 const txt = await helloWorld()
8 console.log(`async func: `, txt)
9 })()
target设置成es5
1 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9 };
10 var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36 };
37 var _this = this;
38 function helloWorld() {
39 return __awaiter(this, void 0, void 0, function () {
40 var res, txt;
41 return __generator(this, function (_a) {
42 switch (_a.label) {
43 case 0: return [4 /*yield*/, fetch('/static/data.json')];
44 case 1:
45 res = _a.sent();
46 return [4 /*yield*/, res.text()];
47 case 2:
48 txt = _a.sent();
49 return [2 /*return*/, txt];
50 }
51 });
52 });
53 }
54 (function () { return __awaiter(_this, void 0, void 0, function () {
55 var txt;
56 return __generator(this, function (_a) {
57 switch (_a.label) {
58 case 0: return [4 /*yield*/, helloWorld()];
59 case 1:
60 txt = _a.sent();
61 console.log("async func: ", txt);
62 return [2 /*return*/];
63 }
64 });
65 }); })();
View Code
target设置成es6
1 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9 };
10 function helloWorld() {
11 return __awaiter(this, void 0, void 0, function* () {
12 const res = yield fetch('/static/data.json');
13 const txt = yield res.text();
14 return txt;
15 });
16 }
17 (() => __awaiter(this, void 0, void 0, function* () {
18 const txt = yield helloWorld();
19 console.log(`async func: `, txt);
20 }))();
View Code
target设置成esnext
async function helloWorld() {
const res = await fetch('/static/data.json');
const txt = await res.text();
return txt;
}
(async () => {
const txt = await helloWorld();
console.log(`async func: `, txt);
})();
View Code
module举例
ts文件中的源代码
设置为commonjs
设置为es6
|
请发表评论