安裝與調試

配置 npm 淘寶源

npm config set registry https://registry.npm.taobao.org/

如果後悔了,想撤銷淘寶源就運行 npm config delete registry

安裝

npm install [email protected] -g
npm install [email protected] -g
想嘗試新版本的同學可以去掉 @2.9.2 和 @7.0.0
我們的課程在 TypeScript 3 上也可以運行

注意記下 ts-node 安裝後的可執行文件路徑,後面要用的。

<figcaption style="margin: 0px; padding: 0px; box-sizing: border-box; display: block; text-align: center;">複製一下路徑</figcaption>

調試

  1. 下載 vscode

    1. 按 ctrl+K ctrl+S
    2. 將格式化文件的快捷鍵綁定到自己喜歡的按鍵(我用的是 ctrl+L)
  2. 創建文件夾 tsdemo

  3. 用 vscode 打開 tsdemo 目錄

  4. 創建 tsdemo/1.ts 作爲我們的第一個 TS 文件

  5. 在文件裏寫一句 console.log(1) 保存

  6. Windows 用戶注意了,這裏需要單獨運行一些命令(Linux 用戶和 macOS 用戶不用執行)

     npm init -y
     npm i -D ts-node typescript
    
    
  7. 創建 tsdemo/.vscode/launch.json 文件,內容如下

     {
         "configurations": [
             {
             "name": "ts-node",
             "type": "node",
             "request": "launch",
             "program": "注意看這裏,要寫成ts-node對應的可執行文件,Windows 用戶注意了,你應該寫成 ${workspaceRoot}/node_modules/ts-node/dist/bin.js", // which ts-node 複製進去即可
             "args": ["${relativeFile}"],
             "cwd": "${workspaceRoot}",
             "protocol": "inspector"
             }
         ]
     }
    
    
  8. 打開 tsdemo/1.js,找到調試選項,選擇 ts-node,然後點擊調試


    <figcaption style="margin: 0px; padding: 0px; box-sizing: border-box; display: block; text-align: center;">截圖</figcaption>

  9. 然後你就可以看到 console.log(1) 的輸入結果了(請確保你選中的是 tsdemo/1.ts)


    <figcaption style="margin: 0px; padding: 0px; box-sizing: border-box; display: block; text-align: center;">Jietu20180716-194457.png</figcaption>

參考文章:https://segmentfault.com/a/1190000011935122

開始學習

  1. 前置知識
    1. 你要對 npm 比較熟悉
    2. 你要對命令行比較熟悉
    3. 你要對 ES 6 比較熟悉
    4. 你要對 class 比較熟悉
  2. 學習一下官方的五分鐘教程
  3. 刻意練習
    1. 大概瀏覽一下文檔
    2. 一些簡單的計算
    3. 一些簡單的應用
    4. 邊練習邊查文檔
enum Gender {
  Male,
  Female
}
interface Person {
  gender: Gender;
}

function merry(a: Person, b: Person): [Person, Person] {
  if (a.gender !== b.gender) {
    return [a, b];
  } else {
    throw new Error('性別相同不能結婚');
  }
}

let a = { gender: Gender.Male };
let b = { gender: Gender.Male };
console.log(merry(a, b));

function selectSort(a: number[]): number[]{
  for(let i =0;i<a.length-1;i++){
    let minIndex = i;
    for(let j = i+1; j<a.length; j++){
      if(a[j]<a[minIndex]){
        minIndex = j
      }
    }
    let temp = a[minIndex]
    a[minIndex] = a[i]
    a[i] = temp
  }
  return a
}

let b = selectSort([100,4,50,1,3])
console.log(b)

function add(a: string, b: string): string;
function add(a: number, b: number): number;
function add(a: any, b: any): any {
  return a + b;
}

console.log(add('frank',1))

function min(a:number, b:number): number {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

var c = min(1, 2);
console.log(c);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章