zoukankan      html  css  js  c++  java
  • typescript 入门例子 Hello world——ts就是一个宿主机语言

    安装 TypeScript

    TypeScript 的命令行工具安装方法如下:

    npm install -g typescript
    

    安装完成之后,就有了 tsc 命令。编译一个 TypeScript 文件很简单:

    tsc hello.ts
    

    我们约定使用 TypeScript 编写的文件以 .ts 为后缀。

    Hello TypeScript

    我们从一个简单的例子开始。

    将以下代码复制到 hello.ts 中:

    function sayHello(person: string) {
        return 'Hello, ' + person;
    }
    
    let user = 'Xcat Liu';
    console.log(sayHello(user));
    

    然后执行

    tsc hello.ts
    

    这时候会生成一个编译好的文件 hello.js

    function sayHello(person) {
        return 'Hello, ' + person;
    }
    var user = 'Xcat Liu';
    console.log(sayHello(user));

    运行:
    node hello.js
    即可看到结果!



    前端使用的话:

    <script type="text/typescript">
    var hw:string="Hello World!"; //定义一个字符串变量
    document.write(<h1>"+hw+"</h1>); //将结果显示在页面上,这句话是不是很熟悉呢。

    如果想在页面上直接编译看到结果,还需要引用typescript.min.js与typescript.compile.min.js。如下代码:
    <html>
    <head>
    <title>demo</title>
    </head>
    <body>
    <script type="text/typescript">
    // TypeScript代码
    </script>
    <script src="lib/typescript.min.js"></script>
    <script src="lib/typescript.compile.min.js"></script>
    </body>
    </html>

     
  • 相关阅读:
    跳出iframe
    leetcode 225. Implement Stack using Queues
    leetcode 206. Reverse Linked List
    leetcode 205. Isomorphic Strings
    leetcode 203. Remove Linked List Elements
    leetcode 198. House Robber
    leetcode 190. Reverse Bits
    leetcode leetcode 783. Minimum Distance Between BST Nodes
    leetcode 202. Happy Number
    leetcode 389. Find the Difference
  • 原文地址:https://www.cnblogs.com/bonelee/p/6841217.html
Copyright © 2011-2022 走看看