{%hackmd tXTfhDH6QTWNg5QB_ZtSuw %} # typescript及IO(std) ---- ## why? ---- #### better IDE ![](https://i.imgur.com/A6E26FP.png) ---- #### better IDE ![](https://i.imgur.com/6J5mUGd.png) --- ## [The TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/intro.html) ## [#syntax] #### ~~給已經有js基礎的人~~ ---- #### javascript ```javascript= let x; x=1; ``` ---- #### typescript ```typescript= let x:number; x=1; // basic type: literal ``` ---- #### javascript ```javascript= let person; person={name:"Eason"}; ``` ---- #### typescript ```typescript= interface Person{ name:string; } let person; person={name:"Eason"}; ``` ---- #### javascript ```javascript= class point{ constructor(x,y){ this.x=x; this.y=y; } distance(p){ return Math.pow((this.x-p.x)**2 +(this.y-p.y)**2 ,0.5); } } ``` ---- #### typescript ```typescript= class point(){ x:number; y:number; constructor(){ this.x=x;this.y=y; } public distance(p:point):number{ return Math.pow((this.x-p.x)**2 +(this.y-p.y)**2, 0.5); } } ``` ---- ## typescript新增 ---- ## 泛型 ```typescript= type a=string; type b=number[]; type c=Uint8Array; type Example1 = Dog extends Animal ? number : string; function identity<Type>(arg: Type): Type { return arg; } ``` ---- ## 過載 ```typescript= function makeDate(timestamp: number): Date; function makeDate(m: number, d: number, y: number): Date; function makeDate(mOrTimestamp: number, d?: number, y?: number): Date { if (d !== undefined && y !== undefined) { return new Date(y, mOrTimestamp, d); } else { return new Date(mOrTimestamp); } } const d1 = makeDate(12345678); const d2 = makeDate(5, 5, 5); const d3 = makeDate(1, 3); ``` --- ## [swc](https://swc.rs/) ---- ### setup ```shell # Download prebuilt binaries npm i -D @swc/cli @swc/core # Transpile JavaScript file and emit to stdout npx swc ./file.js ``` ---- ### config https://swc.rs/docs/configuration/swcrc ---- ```json { "env": { "targets": { "node": "14" } }, "jsc": { "parser": { "syntax": "typescript", "target": "es2019", "dynamicImport": true }, "target": "es2019", "keepClassNames": true, "loose": true }, "module": { "type": "commonjs" } } ``` --- ## [deno](https://deno.land/) --- ## std #### ~~自己看Doc~~ ---- ### basic https://doc.deno.land/deno/stable/~/Deno.writeTextFile https://doc.deno.land/deno/stable/~/Deno.readTextFile https://doc.deno.land/deno/stable/~/Deno.env https://doc.deno.land/deno/stable/~/Deno.create https://doc.deno.land/deno/stable/~/Deno.mkdir ---- ### low level API https://doc.deno.land/deno/stable/~/Deno.open https://doc.deno.land/deno/stable/~/Deno.seek https://doc.deno.land/deno/stable/~/Deno.read ---- ### work with stream and buffer https://doc.deno.land/deno/stable/~/ReadableStream https://doc.deno.land/deno/stable/~/Deno.Reader https://doc.deno.land/deno/stable/~/Deno.writeAll https://doc.deno.land/deno/stable/~/Deno.copy https://doc.deno.land/deno/stable/~/Deno.Buffer ---- ### sub process https://doc.deno.land/deno/stable/~/Deno.run https://doc.deno.land/deno/stable/~/Deno.Process https://doc.deno.land/deno/stable/~/Deno.kill ---- ### 補充 ``` Process.stdout是一個ReadableStream Process.stdin是一個WritableStream Deno是一個Process的子類別 Deno.run的回傳值也是 這樣就可以操作檔案和控制台的輸入輸出了 ``` ---- ## 練習 - 檔案練習 約耗時4小時 下載[Binary](https://www.mediafire.com/file/41egx3v511d93cs/binary/file) 可以嘗試猜猜這是什麼,格式可以找講師要 --- ## module ---- ### 未來會講 - https://deno.land/x/oak - https://deno.land/x/superoak - https://eveningkid.com/denodb-docs/ - https://harmony.mod.land/ --- ## 急救教材 #### ~~給javascript新手~~ ---- - JavaScript大全(第六版) - 找講師急救
{"metaMigratedAt":"2023-06-16T20:45:00.080Z","metaMigratedFrom":"YAML","title":"typescript及IO(std)","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"6d8efe8e-dbdf-4e5a-8c38-356b0d1df76d\",\"add\":4469,\"del\":578}]"}
    409 views
   Owned this note