Share
Embed
Transcript
140 slides
-
About me - ໊শɿݟ༸ี ʢ@Quramy / ͘ΒΈʔ ͱݺΕΕΔ͜ͱ͕ଟ͍ʣ - ॴଐɿPE෦
ASG, ϑϩϯτΤϯυΤϯδχΞ - ৬ྺɿRTCʹདྷͨͷ2020/2݄ɻ ࠃSIer -> Webܥϕϯνϟʔ2ࣾʹ͍·ͨ͠ -
ͱTypeScript - 20153݄5͔ΒTSͬͯ·͢ - ࣌VSCͳ͔ͬͨͷ͋ͬͯɺvim͚ͷTypeScript pluginΛॻ͍ͨΓ - ࠷ۙESLint͚ͷΤσΟλ֦ுGarphQL༻ͷΤσΟλ֦ுΛ࡞ͬͨΓͯ͠ ͍·͢ɻASTܥͷٕज़ׂ͕ͱಘҙͰ͢ -
https://github.com/microsoft/TypeScript-Website ͰTSຊՈαΠτͷ༁׆ಈ ͍ͯ͠·͢ɻίϯτϦϏϡʔτ͍ͨ͠ɺͱ͍͏ਓ͕͍ͨΒ͔͚͍ͯͩ͘͞ - ࠓ2݄։࠵༧ఆͩͬͨTS Conf JPʹCFP௨ͬͨΜ͚ͩͲɺcovid19ͷ͍ͤͰྲྀ Εͨɻɻɻ -
࣍ 1. TypeScript is Կ 2. TypeScriptʹ৮ΕΔ ※ 3. ܕͷૢ࡞
※ 4. ϥΠϒϥϦͷܕఆٛ 5. TypeScriptͱϞδϡʔϧ 6. TypeScriptͱϏϧυΤίγεςϜ ※ -
͍ͬͺ͍͋ΔͧAltJS - AltJS͍Ζ͍Ζ - Flow Type, CoffeeScript, Dart, Reason, Hexa,
Elm, JSX (not React) , ScalaJS, ClojureScript, asm.js, GopherJS, GWT, etc… - ϒϥβͰಈ͔ͤΔݴޠجຊతʹJavaScript͚ͩͳͷͰ※1 ɺੈͷதʹ ͍ͬͺ͍Alt JS͕͋Γ·͢ - ※1 ࠷ۙWebAssembly͋Δ͚ͲɺͦΕআ֎ͨ͠ͱͯ͠ - Ұ࣌ظӍޙͷͷࢠͷ͝ͱ͘ཚཱ͍ͯ͠·͕ͨ͠ɺ࠷ۙTypeScriptʹ ܉্͕͕Γͭͭ͋Δؾ 10 -
·ͣ৮ͬͯΈΑ͏ - TypeScript Playground ʹ͓खܰڥ͕͋Γ·͢ - ৮ͬͯΈΑ͏Playgroundɿ - Run /
LogsͰ࣮ߦΛ֬ೝ͠Α͏ - JSλϒ͔ΒτϥϯεύΠϧ͞ΕͨJavaScriptίʔυΛ֬ೝ͠Α͏ 12 -
ܕνΣοΫ͕Ͱ͖Δ - TypeScript੩తʹܕΛ༩ͯ͠ɺhoge.foo() ͕࣮ߦͰ͖Δ͔Ͳ͏͔ ΛιʔείʔυͷϨϕϧͰνΣοΫ͠·͢ - ʮܕΤϥʔͰౖͬͯΒ͑Δʯͷͤͳ͜ͱͰ͢ - ςετ͕ޮతʹʢςετ͠ͳ͍͍ͯ͘ͱ͍͏༁Ͱͳ͍ʣ -
ಈతݴޠʹܕΛ༩͑ͯɺ੩తʹνΣοΫͰ͖ΔΑ͏ʹ͢Δ͜ͱΛ࣍త ܕʢGradual typingsʣͱ͍͍·͢ 15 -
ܕνΣοΫબΔ - TypeScript as ࣍తܕͷΑ͍ͱ͜Ζɿ - طଘͷJavaScript͚ͩͰॻ͔ΕͨιʔείʔυʹঃʑʹܕΛ͚͍͚ͯΔ - ৽نϓϩδΣΫτͰɺ࠷ॳ͔Β੩తܕݴޠͱಉ͡Α͏ʹ͏͜ͱ Ͱ͖Δ
- TypeScript as ࣍తܕͷҙɿ - ॴḨޙͷػߏɻJavaScriptͷࣗ༝ͱ૬͢ΔՕॴ͋Δ͠ɺӕͭ ͍ͯܕγεςϜ͕ὃ͞ΕΔ͜ͱ͢Β͋Γಘ·͢ 16 -
ΤσΟλαϙʔτ͕༏ल - TypeScriptͦͷͷʹҎԼͷػೳؚ͕·Ε͍ͯΔͷͰɺΤσΟλ͕շదͰ͢ - ΤϥʔνΣοΫ - ิ - ఆٛδϟϯϓ -
ࢀরՕॴͷදࣔ - ϦϑΝΫλϦϯάิॿػೳ - etc… 19 -
ΤσΟλαϙʔτ͕༏ल - ʢ༨ஊʣ - TypeScriptͷݴޠαʔϏεʢtsserverʣC#ͷΈΛݩʹɺ LSP(Language Service Protocol) ͱ͍͏ʮΤσΟλͱϓϩάϥϛϯά ݴޠؒͷΠϯλʔϑΣʔεʯ͕ੜ·Εͨ΄Ͳ
- ೦ͳ͜ͱʹTypeScriptLSP४ڌ͍ͯ͠·ͤΜ 20 -
Function parameters - ؔͷԾҾʹऍΛ༩ͯ͠Έ·͢ - PlaygroundͰԼهΛॻ͍ͯΈͯɿ function add(a: number, b:
number) { return a + b; } console.log(add(100, 100)); 27 -
ऍͰ͖Δͱ͜Ζ - ଞʹ : ܕ໊ ͰऍͰ͖Δͱ͜Ζ৭ʑ͋Γ·͢ const a: string =
"hoge"; let b: string; var c: string; function d(): string { return "hoge"; } class MyClass { e: string = ""; } 30 -
ऍͱਪͷিಥ - ऍͨ͠ܕͱਪ݁Ռ͕৯͍ҧ͏ͱɺίϯύΠϧΤϥʔʹͳΓ·͢ - ԼهͷίʔυͰΤϥʔʹͳΔ͜ͱΛ֬ೝͯ͠Έͯɿ function add(a: number, b: number)
{ return a + b; } const a: string = add(100, 100); 31 -
Primitive types - Ҏ֎ʹɺจࣈྻਅِͳͲprimitive typeͱͯ͠༻ҙ͞Ε͍ͯ ·͢ const bool: boolean =
true; const num: number = 100; const str: string = "hogehoge"; const nullObj: null = null; const undefVal: undefined = undefined; 32 -
Function type - JavaScriptͷੈքͰɺؔΦϒδΣΫτͷҰछͰ͋Δ͜ͱ͔Βɺ TypeScriptʹʮؔࣗମͷܕʯ͕͋Γ·͢ - ྫɿ - Γ͕ଘࡏ͠ͳ͍ؔͷ߹ɺ=> void
ͱॻ͖·͢ (a: number, b: number) => number 33 -
Object / Array type const obj: { name: string, age:
number } = { name: "Bob", age: 20, }; const arr: number[] = [1, 2, 3]; 34 -
Type alias - typeߏจͰܕʹผ໊Λ͚ͯ࠶ར༻Ͱ͖·͢ɻ”type alias” ͱݺΕ· ͢ɻ͔ͳΓසൟʹ͍·͢ - type໊େจࣈ͔Β։࢝͢Δͷ͕ҰൠతͰ͢ type
Messages = string[]; function showMessages(msgs: Messages) { msgs.forEach(msg => console.log(msg)); } 35 -
ϛχԋश - ҎԼΛຬͨ͢ GetUser ͱ͍͏ؔܕΛॻ͍ͯΈͯ - input: จࣈྻܕ - output:
࣍ͷߏͰఆٛ͞ΕΔΑ͏ͳUserܕ id จࣈྻ name จࣈྻ age isMarried ਅِ 36 -
Class type TodoItem = { name: string; }; interface Service
{ getTodos(): TodoItem[]; } class ConcreteService implements Service { private _todos: TodoItem[]; constructor(initialTodos: TodoItem[]) { this._todos = initialTodos; } getTodos() { return this._todos; } } 38 -
JavaScriptͷClassʹແ͍ػೳ - ϑΟʔϧυɾϝιουʹՄࢹੑ͕ઃఆͰ͖Δ - private / protected / public -
※ TypeScriptʹ͓͚ΔprivateߏจSoft PrivateɻͦͷؾʹͳΕΞΫ ηεͰ͖Δ - நΫϥε / நϝιουΛఆٛͰ͖Δ - InterfaceΛ࣮Ͱ͖Δ 39 -
any - ʮͯ͢ͷܕͱͯ͠ৼΔ͏͜ͱ͕Ͱ͖ΔʯͨΊɺܕʹؔ͢Δίϯύ ΠϧΤϥʔ͕ͯ͢ࢭ͞Ε·͢ - anyTypeScriptʹ͓͚ΔඞཁѱͰ͢ɻ͏ͱ͖ࡑѱײͰ৺Λগ͠௧ ΊΔΑ͏ʹ͠·͠ΐ͏ const fn: any
= () => 100; const hoge: number = fn; 41 -
anyΛ͏ͷ͕ଥͳέʔε - anyੑ࣭Λཧղͯ͠͏ʹศརͳػೳͰ͢ - طଘͷJavaScriptϓϩδΣΫτTypeScriptΛಋೖ͢ΔࡍʹɺͱΓ͋͑ ͣܕऍΛͯ͠͠·͍͍ͨʢ࣮ίʔυΛഁյͨ͘͠ͳ͍ʣ - ෳࡶͳؔͷ෦ͰҰ࣌తʹ͏έʔεɻؔͷςετΛ͔ͬ͠Γͱ ߦ͍ͬͯͯɺ͔ͭҾͱΓͷܕ͕ͪΌΜͱॻ͍ͯ͋Εͳ͍ -
͍ͣΕʹͤΑʮanyͷӨڹہॴతʹԡ͓͑ͯ͘͞ʯ͕TypeScriptͱ͏· ͖͘߹͍ͬͯ͘ίπͰ͢ 42 -
ೖՄೳͱԿ͔ - assignableͷྫ: - showMessageؔɺจࣈྻܕͰ͋Δmessageͱ͍͏ϑΟʔϧυΛ ࣋ͬͨܕͰ͋ΕɺԿͰೖՄೳ type SomeType = {
message: string; }; function showMessage(obj: SomeType) { console.log(obj.message); } showMessage({ message: “Hello!" }); 44 -
Interface v.s. Type alias - ϑΟʔϧυϝιουΛ࣋ͭΦϒδΣΫτͷܕΛఆٛͰ͖Δͱ͍͏ҙຯ Ͱɺtype aliasͱinterfaceʹػೳͷॏෳ͕͋Γ·͢ interface IHoge
{ message: string; onChangeMessage(cb: (msg: string) => void): void; } type HogeType = { message: string; onChangeMessage(cb: (msg: string) => void): void; }; let hoge1: IHoge; let hoge2: HogeType; /* IHogeͱHogeTypeํʹassignable */ hoge1 = hoge2; hoge2 = hoge1; 47 -
Interface v.s. Type alias - લϖʔδͷ IHoge interface ͱ HogeType
ʹ͍ͭͯɺPlayground্Ͱ ͷҧ͍Λ୳ͯ͠Έͯ - લϖʔδͷྫʹɺԼهΛهͨ͠ͱ͖ʹͲ͏ͳΔ͔֬ೝͯ͠Έͯ interface IHoge { errorCode: number; } 48 -
ԋशͷલʹ - ։ൃڥ͕͍ͬͯΔ͜ͱΛ֬ೝ͠·͠ΐ͏ - ҎԼͷίϚϯυར༻ՄೳͰ͔͢ʁ - git - node --version
- ΤσΟλ: - ͩ͜ΘΓ͕ແ͚ΕɺVisual Studio Code Λ͍ͬͯͩ͘͞ 51 -
ԋशͷΓํ - repositoryΛclone - https://github.com/recruit-tech/bootcamp-2020-ts.git - cd server - npm
i typescript -D - npm i @types/express @types/body-parser -D - npx run tsc —init - .jsϑΝΠϧΛ.tsϑΝΠϧʹϦωʔϜ͢Δ 53 +4ฤͰ࡞Γ͖Ε͍ͯΔͷͰ͋Εɺ ͦͷίʔυΛͬͯ ͜ͷίϚϯυʹ͍ͭͯޙͰղઆ͠ ·͢ -
ʢิʣtsconfig.jsonʹ͍ͭͯ - TypeScriptͷઃఆϓϩδΣΫτϧʔτԼͷtsconfig.jsonʹهड़͠·͢ - ͜ͷԋशͰॻ͖͑ΔՕॴɿ - target: “es2020” - ࠔͬͨΒ
https://www.typescriptlang.org/v2/ja/tsconfig ΛಡΜͰͩ͘͞ ͍ 55 -
ʢิʣrequireͱimport - Node.jsͷrequireߏจͷΘΓʹɺES2015ͷimportจΛ͏Α͏ʹͯ͠ ͍ͩ͘͞ - TypeScriptʹ͓͚ΔʮϞδϡʔϧʯʹ͍ͭͯผ్ޙड़͠·͢ const express = require("express");
import express from "express"; 56 module.exports = router; export default router; -
Type error - JavaScriptͷϓϩάϥϜͰͬͱසൟʹੜ͡Δόάɺnull ·ͨະఆ ٛͷมʹରͯ͠ɺ͕ଘࡏ͢ΔલఏͰΞΫηεͯ͠͠·͏έʔεͰ͢ - ʢ༨ஊʣALGOL WੜΈͷͰ͋ΔTony Hoareᐌ͘ʮNullࢀর࠷େͷ
ޡΓͩͬͨɻίϯύΠϧͰͯ͢νΣοΫͰ͖ΔΑ͏ʹ͔ͨͬͨ͠ʯ - https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony- Hoare/ 59 Uncaught TypeError: Cannot read property ‘hoge' of undefined -
Optional - ΦϓγϣϯͷstrictNullCheck͕trueͷ߹ɺoptionalͳΛ͍͖ͳΓૢ ࡞͠Α͏ͱ͢ΔͱTypeScript͕ίϯύΠϧΤϥʔʹͯ͘͠Ε·͢ ʢίϯύΠϧΤϥʔ͝๙ඒͩͱࢥ͍·͠ΐ͏ʣ function fn(args: { message?: string})
{ // ࣄલʹargs.messageͷଘࡏΛνΣοΫ͢Δඞཁ͕͋Δ if (!args.message) return ""; return args.message.toLowerCase(); } fn({}); // messageΛলུՄೳ 61 -
ͦͷଞͷOptionalදݱ - ϓϩύςΟ໊ޙͷ ? Ҏ֎ʹOptionalมΛදݱ͢Δํ๏͕͋Γ·͢ - ※ ύΠϓه߸ͷҙຯޙड़͠·͢ // ؔͷoptionalҾ
function hoge(a: number, b?: number) { } // null undefined ͱͷUnion type X = string | undefined; type Y = string | null; -
strictNullCheck ʹ͍ͭͯ - strictNullChecksɺࢀর͍ͯ͠Δͷܕʹnull ·ͨ undefined͕ೖ Մೳͳ߹ʹɺͦͷͷૢ࡞Λ੍ݶ͢ΔػೳͰ͢ - TypeScriptͷσϑΥϧτઃఆͰɺstrictNullCheckONͰ͢ -
جຊతʹ༗ޮʹ͓͍ͯͨ͠ํ͕ྑ͍ػೳͰ͕͢ɺطଘͷJavaScriptϓ ϩδΣΫτʹޙͰTypeScriptΛಋೖ͢Δ߹ɺҰ୴offʹ͢ΔͷΞ ϦͰ͢ 63 -
Modifiers - ܕఆٛͰͷ ? optional modifierͱݺΕ·͢ - modifierʹoptionalҎ֎ͷmodifierͱͯ͠ɺreadonlyͱ͍͏ͷ͋Γ· ͢ʢʮSoftͳʯ࣮Ͱ͋ΔͨΊɺτϥϯεύΠϧޙͷjs͔Βॻ͖͑Մೳʣ type
Hoge = { m1: string; m2?: string; readonly m3: string; readonly m4?: string; }; 64 -
Literal type - /จࣈྻ/ਅِܕɺͦΕͧΕͷͦͷͷΛܕͱͯ͠͏͜ͱ͕ Ͱ͖·͢ɻ͜ΕΛLiteral Typeͱݺͼ·͢ type SomeString = "some
string"; type SomeNumber = 1; type True = true; function fn(str: SomeString) { return str.trim(); } fn("some string"); // OK fn("other string"): // Error 65 -
Union type - ෳͷܕΛύΠϓه߸ʢ | ʣͰ۠Δ͜ͱͰɺUnion Typeʢܕ, ڞ༻ମ ܕͱʣΛએݴ͠·͢ -
String literal typeͱซ༻͢Δͱɺྻڍܕͱಉ͡Α͏ʹѻ͏͜ͱͰ͖· ͢ʢTypeScriptʹenumߏจ͕༻ҙ͞Ε͍ͯ·͕͢ɺ͋·Γར༻͞Ε·ͤΜʣ type NumOrStr = string | number; type MaybeStr = string | null | undefined; type Colors = "red" | "blue" | "green" | "yellow"; 66 -
ܕͷߜΓࠐΈ - Union TypeɺifจswitchจͰΛνΣοΫ͍ͯ͘͜͠ͱͰɺΑΓৄࡉ ͳܕߜΓࠐΉ͜ͱ͕Ͱ͖·͢ɻ͜ΕΛType GuardͱΑͼ·͢ - strictNullCheckͰઆ໌ͨ͠ifจʹΑΔߜΓࠐΈType GuardͷҰछͰ͢ function
fn(input: string | number) { console.log(input.toString()); if (typeof input === "number") { console.log(200 * input); } else { console.log(input.trim()); } } 67 -
Type Guardͷछྨ - ͍͔ͭ͘ͷߏจ͕Type Guardͱͯ͠ར༻Ͱ͖·͢ɿ - typeof x === “string”,
x instanceof HogeClass - Literal typeʹ͓͚Δಉ֬ೝʢ if (x === “some message”) ʣ - obj.x obj[“x”] x in objͳͲͷଘࡏ֬ೝ - User Defined Type Guard function: function fn(x: any): x is { msg: string } { return x.msg && typeof x.msg === "string"; } 68 -
ϛχԋश̍ - ҎԼͷؔ type ʹԠͯ͡ɺpayload ͷܕ͕ҟͳΓ·͢ɻ ίϯύΠϧ͕௨ΔΑ͏ʹɺAction ͷܕΛߟ͑ͯΈͯ 69 function
reduce(state = [100], action: Action) { switch (action.type) { case “CONCAT": // payload: number[] return state.concat(action.payload); case “REMOVE": // payload: number return state.filter(val => val !== action.payload); default: return state; } }; -
ϛχԋश̎ - ࣍ͷίʔυԼઢ෦ͰίϯύΠϧΤϥʔͱ͞Εͯ͠·͍·͢ɻԿ ނͰ͠ΐ͏͔ʁ const prices = [1000, 500]; type
Item = { price?: number; }; function fn(item: Item): boolean { if (item.price == null) return false; return prices.some( p => item.price > p, ); } 70 -
Ωϟετ - จࣈྻͷLiteral typestringܕʹೖՄೳͰ͕͢ɺ stringܕจࣈྻͷLiteral typeʹೖͰ͖·ͤΜ - ʮೖՄೳʯɺSub typeʢ۩ʣˠ Supar
typeʢநͳܕʣͷํʹ ݶఆ͞ΕΔ͔ΒͰ͢ type SomeString = "some string"; const x: SomeString = "some string"; const y: string = x; const z: SomeString = y; Supar type Sub type type SomeString string -
Ωϟετ - ͜ͷΑ͏ͳ߹ɺมͷޙΖʹ as ܕ໊ Λ༩͢Δ͜ͱͰܕΛมͰ͖ ·͢ʢਖ਼ࣜʹ ΩϟετͰͳ͘ɺType assetion ͱݺΕΔػೳʣ
Supar type Sub type type SomeString string type SomeString = "some string"; const x: SomeString = "some string"; const y: string = x; const z = y as SomeString; -
asͷ੍ - asͰมͰ͖ΔͷɺSupar type → Sub type ͷ͕݅ຬͨ͞ΕΔͱ͖ ͷΈͰ͢ -
e.g. numberܕstringܕʹมͰ͖·ͤΜ Supar type Sub type type SomeString string -
Intersection type - Union typeܕͱܕͷͰ͕͢ɺܕͱܕͷੵଘࡏ͠·͢ - ه߸ & ͰෳͷܕΛ࿈݁͢ΔͱɺIntersection typeʹͳΓ·͢
- type A & B ɺʮAʹBʹassignableʯͳܕͰ͢ 74 -
Bottom type - neverܕʮͯ͢ͷܕͷsubtypeʯͱͯ͠ѻΘΕ·͢ɻͯ͢ͷܕ͕ neverೖͰ͖·͕͢ɺneverneverҎ֎ͷͯ͢ͷܕʹೖͰ͖· ͤΜ - Type Guardͷ݁Ռɺneverܕʹਪ͞ΕΔϒϩοΫʮ౸ୡෆՄೳʯͱ ͍͏ҙຯʹͳΓ·͢
function fn(x: number) { if (typeof x === "string") { return x.trim(); } return x; } 75 -
Composition never - Լهͷܕ A ɺB ͦΕͧΕͲΜͳܕͰ͠ΐ͏ʁ (XԿ͔దͳܕͱ͠·͢ʣ type A
= never | X; type B = never & X; 77 -
Generics type List<T> = T[]; function getLength<T>(list: List<T>) { list.length;
} interface IFoo<T> { list: ReadonlyArray<T>; } class Foo<T> implements IFoo<T> { constructor(public readonly list: T[]){ } get size() { return getLength(this.list); } } const foo = new Foo<string>([""]); 80 -
Generics // Γ͕Promise<{ id: number; name: string; }> function getTodo()
{ return Promise.resolve({ id: 100, name: "hoge", }); } const index = new Map<number, string>(); index.set(1, "hoge"); index.has(1); const colors: ReadonlyArray<string> = [ "red", "green", "blue", ]; colors.sort(); // ഁյૢ࡞ېࢭ -
ԋशͷΓํ - repositoryΛcloneʢԋश1ͱಉ͡ͱ͜Ͱ͢ - https://github.com/recruit-tech/bootcamp-2020-ts.git - cd client - npm
init -y - npm i typescript -D - npx run tsc —init - js/**/*.jsΛ.tsʹॻ͖͑Δ - npx run tsc --watch 85 -
ʢิʣimport / export - TypeScriptʹ͓͚ΔʮϞδϡʔϧʯʹ͍ͭͯผ్ޙड़͠·͢ - ͜ͷԋशͰɺES2015ͷ import / export
ߏจ͕ͦͷ··ར༻Ͱ͖· ͢ɻಛʹ import จ export จΛॻ͖͑Δඞཁ͋Γ·ͤΜ 87 -
ʢิʣඪ४ APIͱTypeScript - TypeScript ࣗମʹඪ४ APIͷܕ͕όϯυϧ͞Ε͍ͯ·͢ - JavaScriptʢECMAScriptʣͷඪ४ API -
DOM APIfetchͳͲɺϒϥβʹඪ४ͰඋΘ͍ͬͯΔAPI 88 -
;Γ͔͑Γ - ԋश1 ͱ ԋश2 Λ௨ͯ͠ɺαʔόʔαΠυɺϒϥβαΠυͷίʔυΛ ܕͰकΔ͜ͱͰ͖·ͨ͠ - ͱ͜ΖͰɺϒϥβͱαʔόʔ͕௨৴͍ͯ͠Δ෦Ͳ͏Ͱ͠ΐ͏͔ʁ -
req.bodyʢαʔόʔଆʣɺfetchͷΓʢϒϥβଆʣʹҰ؏ੑ ͋Γ·͔͢ʁ -
Advanced type - ࠓׂѪ͠·͕͢ɺܕγεςϜʹΑΓߴͳػೳ༻ҙ͞Ε͍ͯ· ͢ - Mapped type: objectܕͷkeyΛ࠶ར༻ͯ͠ผͷܕΛ࡞Δ -
Conditional type: ೖՄೳੑͰܕΛύλʔϯϚον - Type inference in conditional type: ύλʔϯϚονͨ͠ܕͷ࠶ར༻ 91 -
Advanced type interface Observable<T> { subscribe(cb: (v: T) => void):
void; } type Unboxing<T> = T extends Observable<infer S> ? S : never; declare function combineLatest<U extends Observable<any>[]>( ...args: U ): Observable< { [P in keyof U]: Unboxing<U[P]>; } >; declare const s1: Observable<string>; declare const s2: Observable<number>; declare const s3: Observable<boolean>; combineLatest(s1, s2, s3).subscribe(([v1, v2, v3]) => { }); -
1. NPM ύοέʔδʹܕఆ͕ٛಉ ࠝ͞Ε͍ͯΔ - npm install (ϥΠϒϥϦ໊) Λ࣮ߦ͢Δ͚ͩͰܕఆ͕ٛར༻ՄೳʹͳΓ· ͢
- ͜ͷखͷͭɺϥΠϒϥϦͷpackage.jsonʹ “types”: “index.d.ts” ͱ ͍ͬͨهड़͕͋Γ·͢ 100 -
ܕఆٛϑΝΠϧͷҙ - ఏڙ͞Εͨܕఆ͕ٛຊମͷAPIΛཏ͍ͯ͠ͳ͍͜ͱɺ··͋Γ·͢ - e.g. ಛఆͷؔͷܕఆ͕ٛͬͦ͝Γൈ͚ͯΔ / anyͩΒ͚ / etc…
- ͜ͷखͷέʔεʹ໘ͨ͠ΒɺܕఆٛΛमਖ਼͢ΔPRΛग़͠·͠ΐ͏ 104 -
Ճ՝ - ԋश1͕ૣ͘ΘͪΌͬͨਓ͚ - expressʹϛυϧΣΞͱ͍͏ΈͰɺϢʔβʔଆͰ Request / ResponseͷΦϒδΣΫτΛ֦ுͰ͖·͢ const app
= express(); app.use((req, res, next) => { req.isAuthenticated = true; next(); }); app.get("/api/v1/todos", (req, res) => { console.log(req.isAuthenticated); // ͦͷଞͷॲཧ }); ͜ͷ෦͕ϛυϧΣΞ ϛυϧΣΞͰՃ͞ΕͨϑΟʔϧυ 105 -
ES2015 Ϟδϡʔϧ // calc.js // ϞδϡʔϧΛར༻͢Δଆ import { add }
from “./add.js”; console.log(add(1, 2)); // add.js // ϞδϡʔϧΛఆٛ͢Δଆ export function add(a, b) { return a + b; } -
CommonJSͷϞδϡʔϧ // calc.js // ϞδϡʔϧΛར༻͢Δଆ const add = require("./add"); console.log(add(1,
2)); // add.js // ϞδϡʔϧΛఆٛ͢Δଆ function add(a, b) { return a + b; } module.exports = add; -
Module loader - ES2015 ϞδϡʔϧͰ͋ΕɺಠࣗܗࣜͰ͋ΕɺϞδϡʔϧԽ͞Εͨ JavaScriptΛऔΓࠐΉΈ͕ඞཁͰ͢ - <script type=“module”>λάɺNode.jsͷrequireػߏͳͲ -
ϞδϡʔϧΛಡΈࠐΉΈʮϞδϡʔϧϩʔμʔʯͱݺΕ·͢ 112 -
--module - moduleઃఆͷ͍͚ྫ - ԋश1ͰɺNode.jsͰಈ࡞ͤ͞ΔͨΊʹ module: “commonjs”Λઃ ఆ - ԋश2Ͱɺϒϥβͷ<script
type=“module”>ػೳΛͬͯಡΈ ࠐΉͨΊʹɺ module: “esnext”Λઃఆ 114 -
ʢ༨ஊʣଞͷ--moduleୡ - --module ʹ “amd” / “umd” / “system” ͷબՄೳͰ͢ɻ…͕໓ଟ
ʹ͏͜ͱͳ͍Ͱ͢ - ڵຯ͕͋ͬͨΒɺ“RequireJS” ͱ͔ “SystemJS” ͰௐͯΈΔͱྑ͍ͱࢥ ͍·͢ 115 -
TypeScriptͱͯ͠ͷϞδϡʔϧ - Ϟδϡʔϧͱͯ͠ղऍ͞ΕΔ.tsϑΝΠϧ - ϞδϡʔϧʹͳΒͳ͍.tsϑΝΠϧ /* types.ts */ // Todo
ΠϯλʔϑΣΠεͲ͔͜ΒͰࢀরՄೳ interface Todo { name: string; } /* types.ts */ // Todo ΠϯλʔϑΣΠε import ͠ͳ͍ͱࢀরͰ͖ͳ͍ export interface Todo { name: string; } 117 -
Ϗϧυπʔϧͷཚཱ - Ϟδϡʔϧϩʔμʔ - CommonJS (Node.js) ελΠϧ, RequireJSελΠϧ, etc… -
ϏϧυγεςϜ - Grant, Gulp, etc… - ѹॖʢಡԽʣπʔϧ - YUI Compressor, ClosureCompiler, UglifyJS, etc… 121 -
Ϗϧυπʔϧͷཚཱ - Ϟδϡʔϧϩʔμʔ - CommonJS (Node.js) ελΠϧ, RequireJSελΠϧ, etc… -
ϏϧυγεςϜ - Grant, Gulp, etc… - ѹॖʢಡԽʣπʔϧ - YUI Compressor, ClosureCompiler, UglifyJS, etc… 122 ͱʹ͔͘৭ʑ͋ͬͨʂ -
Module bundler - αʔόʔαΠυJavaScriptͰ͋ΔNode.jsͷීٴͱɺ Node.jsʹ͓͚ΔϞδϡʔϧػߏʹΠϯεύΠΞ͞Ε ͯɺbrowserifyͱ͍͏πʔϧ͕ҰੈΛ෩ᴆ͠·ͨ͠ - ιʔείʔυதͷ require(‘…’) Λ੩తղੳͰ୳ࡧ͠ɺ
require͞Ε͍ͯΔґଘscriptΛ݁߹͢Δ͜ͱͰɺ୯Ұ ͷ.jsϑΝΠϧΛੜ͠·͢ 123 -
webpack - webpack ۙ͘ΘΕ͍ͯΔϞδϡʔϧόϯυϥͰ͢ - https://webpack.js.org/ - JavaScript / AltJS͚ͩͰͳ͘ɺը૾CSSͳͲ༷ʑͳϑΝΠ
ϧΛΞηοτͱͯ͠όϯυϧϑΝΠϧʹऔΓࠐΊ·͢ 125 -
webpack - webpackͰͰ͖Δ͜ͱʢҰ෦ൈਮʣ: - AltJSΛτϥϯεύΠϧͭͭ͠ɺόϯυϧΛ࡞͢Δ - όϯυϧʹରԠ͢ΔιʔεϚοϓϑΝΠϧΛ࡞͢Δ - ಡԽΛࢪͯ͠࠷ऴతͳϑΝΠϧαΠζΛখ͘͢͞Δ -
Node.jsͷAPIΛҰ෦ϒϥβ͚ʹఏڙ͢Δ 126 -
࠷ۙͷࣄ - ۙͰɺECMAScriptͷimport/exportߏจϒϥβͷ<script type=“module”>ͱ͍ͬͨඪ४Խ͕ਐΈɺόϯυϥʹཔΒͳ͘ͱϞ δϡʔϧΛར༻Մೳʹͳ͖ͬͯ·ͨ͠ʢIEআ͘ʣ - ͱ͍͑ɺimport ͷ࿈͕ਂ͘ͳΔͱɺϒϥβ͕࠷ޙͷϞδϡʔϧʹ ౸ୡ͢Δ·Ͱʹ͕͔͔࣌ؒͬͯ͠·͍·͢※1 ɻ͜ͷҙຯͰɺϑΝΠϧΛ
·ͱΊΔϞδϡʔϧόϯυϥඞཁͱ͞Ε͚ͭͮΔͰ͠ΐ͏ - ※1 https://docs.google.com/document/d/1ds9EEkcDGnt-iR8SAN-_7nsOfw7gsMfhZjzZ_QAIyjM/ mobilebasic 127 -
ԋशͷΓ͔ͨ - repositoryΛcloneʢԋश2ͱಉ͡ͱ͜Ͱ͢ - https://github.com/recruit-tech/bootcamp-2020-ts.git - cd client - npm
i webpack webpack-cli ts-loader -D 132 -
ESLint - ESLint ASTʢநߏจʣΛ༻͍ͨίʔυͷνΣοΫπʔϧͰ͢ɻې ࢭ͍ͨ͠ίʔυύλʔϯΛϧʔϧͱͯ͠ొͯ͠ར༻͠·͢ - ϧʔϧͷྫ: - consoleΛΘͳ͍͜ͱ -
࠶ೖ͠ͳ͍มʹletͰͳ͘constΛ༻͍Δ͜ͱ - TypeScriptͱESLintΛซ༻͢Δ߹ɺhttps://github.com/typescript- eslint/typescript-eslint Λ͍·͢ -
Prettier - PrettierιʔείʔυΛػցతʹϑΥʔϚοτ͢ΔͨΊͷπʔϧͰ͢ - ԼهͷΑ͏ͳෆໟͳٞʹࠨӈ͞Εͳ͍ͨΊʹɺίϛοτ࣌ʹίʔυ ΛࣗಈͰϑΥʔϚοτ͓ͯ͘͠श׳Λ͚͓ͭͯ͘ͷྑ͍͜ͱͰ͢ - ʮจࣈྻϦςϥϧγϯάϧΫΥʔτ or μϒϧΫΥʔτʯ
- ʮηϛίϩϯΛจͷඌʹ͚ͭΔ͖͔ʯ - Prettierඪ४ͰTypeScriptʹରԠ͍ͯ͠·͢ -
͓ΘΓʹ - ࠓԼهͳͲΛத৺ʹֶΜͰ͖·ͨ͠ - TypeScriptͷجຊจ๏ - طଘͷJavaScriptίʔυʹܕΛಋೖ͢Δํ๏ - TypeScriptͷϓϩδΣΫτʹ͓͚ΔϏϧυͷํ๏ -
࣌ؒͷ߹্ɺ৮Ε͍ͯͳ͍τϐοΫ·ͩࢁ͋Γ·͢ɻؾʹͳΔ ςʔϚ͕͋ͬͨΒɺੋඇࣗͰௐɺʢՄೳͰ͋ΕʣखΛಈ͔ͯ͠Έ ͍ͯͩ͘͞