【TypeScript 】入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/stu_20052369/article/details/87182257

1. 安装TypeScript  安装npm

npm install -g typescript

2. 安装IDE(博主使用的是VS Code

3. 创建第一个TS文件,firstTest.ts

function greeter(person:Person) {
    return "Hello " + person.firstName+" "+ person.lastName+", have a nice day!";
}

interface Person{
    firstName:string;
    lastName:string;
}
let user = {firstName:"Gao",lastName:"Jamie"};
document.body.innerHTML = greeter(user);

4. 使用VS code 的Terminal 窗口编译ts文件。

tsc firstTest.ts

   在同目录下面输出一个同名的.js文件。它包含了和输入文件中相同的JavsScript代码。 如果TS文件的代码里有错误,仍然会输出js文件。但在这种情况下,TypeScript会警告你代码可能不会按预期执行。

5. 使用 js文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    
</head>
<body>
    <script src="firstTest.js"></script>
</body>
</html>

6. 建议TypeScript 使用ESlint 进行代码检查 ESlint的安装与使用

猜你喜欢

转载自blog.csdn.net/stu_20052369/article/details/87182257