TypeScript hello world
hello world
跟其它的大多数计算机语言一样,接下来将通过hello world来编写TypeScript的第一个程序文件。可以在vscode当中,新创建一个后缀为.ts的文件,名称随便,比如test.ts,然后输入如下代码:
function first() {
return "hello world";
}
document.body.innerHTML = first();
在终端中,并进入到test.ts所在的当前目录中,输入运行TypeScript的文件代码,对ts文件进行编译:
tsc test.ts
编译之后,会得到一个test.js的文件,代码如下:
function first() {
return "hello world";
}
document.body.innerHTML = first();
之后,将该js文件引入到html文档之中,如:
<html>
<head>
</head>
<body>
<script src="test.js"></script>
</body>
</html>
然后运行该html,将得到输出:hello world