TypeScript初始化

JS的讲解这里直达

TS准备工作

安装解析ts安装包

nodejs不能直接访问ts要内部把ts转换JS
提供tsc命令 实现ts-js转换
需要在VS里面的终端npm
npm i -g typescript

第一个ts文件

创建一个ts后缀文件名为hello.ts里面写
console.log('hello.ts')
如果进行查看
在终端看二个步骤
tsc hello.ts//这个与你的文件保持一致
node hello.ts//这个命令就会打印出你文件的console值

简化执行TS步骤

使用ts-node包 直接在nodejs执行TS代码

安装ts-node

npm i -g ts-node

安装完这个命令 在内部看输出命令
比如直接

ts-node hello.ts

执行的时候报错显示

'@types/node/package.json'C:\Users\MI\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:692
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
hello.ts:2:5 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

2     console.log('hi'+name);

解决方法

npm install -D tslib @types/node
然后继续在终端查看就可命令还是一样

ts-node hello.ts

TS语法

变量

let age: number=20

Guess you like

Origin blog.csdn.net/m0_53912016/article/details/121419983