使用TypeScript来创建一个简单的Web应用
1、创建文件01-test文件夹,VScode打开文件夹
2、npm install typescript -g 全局安装TypeScript
3、创建test.ts文件 (可以写测试的ts代码)
const person = {
name: 'shenjiu',
age: 18
}
console.log(`my name is ${person.name}, age is ${person.age}`)
4、tsc --init 再根目录下创建 tsconfig.json 配置文件
如果出现报错:
tsc --init
tsc : 无法加载文件 D:\Program Files\nodejs\node_global\tsc.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ tsc --init
+ ~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
解决办法:
1.管理员身份运行vs code
2.在终端执行:get-ExecutionPolicy,显示Restricted
3.在终端执行:set-ExecutionPolicy RemoteSigned
4.在终端执行:get-ExecutionPolicy,显示RemoteSigned
5、tsc --wacth 用watch来动态检测ts文件的改变并自动编译(编译生成对应的js文件test.js)
"use strict";
const person = {
name: 'shenjiu',
age: 18
};
console.log(`my name is ${person.name}, age is ${person.age}`);
6、node test.js
控制台输出:
my name is shenjiu, age is 18