npm install -g typescript
新建一个hello.ts文件
function hello(person:string) { //函数参数这里,必须指定传入的参数是什么类型,否则编译会报错,但是仍然会继续编译
return 'hello' + person
}
let username = '大狗'
console.log(hello(username))
TypeScript 中,使用 : 指定变量的类型,: 的前后有没有空格都可以
运行
tsc hello.ts
会生成一个hello.js文件
function hello(person) {
return 'hello' + person;
}
var username = '大狗';
console.log(hello(username));
TypeScript 编译的时候即使报错了,还是会生成编译结果,我们仍然可以使用这个编译之后的文件。
。
网友评论