0
点赞
收藏
分享

微信扫一扫

TypeScript基础 ts文件的创建与执行

蛇发女妖 2022-04-13 阅读 68

创建

1.新建一个文件夹
2.用vscode打开
3.在文件夹下新建一个hello.ts的文件
在这里插入图片描述

执行

在ts文件中输入代码console.log('111')
在这里插入图片描述

因为node.js不能直接读取ts代码 需要转换成ts文件再进行读取(需要安装typescript包)
在终端 输入 tsc hello.ts node hello.js 即可将ts文件转化成js文件并打印结果

简化执行

如果修改代码如果都执行上方两个代码过于繁琐,所以我们可以采取简化方式 , 安装ts-node包 npm i -g ts-node 使用方式:ts-node hello.ts

使用这个命令可能会报错:
TSError: ⨯ Unable to compile TypeScript:
hello.ts:1:1 - error TS2584: Cannot find name ‘console’. Do you need to change your target library? Try changing the ‘lib’ compiler option to include ‘dom’.
1 console.log(‘111’);
在这里插入图片描述

原因是:版本ts-node的版本太新了
使用 npm uni -g ts-node 卸载掉ts-node
再使用 npm i -g ts-node@8.5.4下载旧一点的版本就不会了.

举报

相关推荐

0 条评论