Share
Embed
Transcript
37 slides
-
荈䊹稱➜ ؿٗٝزؒٝسؒٝآص،倜⼼䎃湡!-*/&吳䒭⠓爡 5ZQF4DSJQU娖䎃⼱ְֻ 2JJUB蔓➂5ZQF4DSJQUةؚDPOUSJCVUJPO侧⡘ ! չ5ZQF4DSJQUך㘗Ⰵպ "չ侁⻌罏ך5ZQF4DSJQUպ
# չ5ZQF4DSJQUך㘗䱿锷鑫铡պ זו !VIZP@ -
5ZQF4DSJQUך㸜Ⰻ䚍ה؝أز 㸜Ⰻ䚍 㘗ךך 㷕统؝أز 涪؝أز 넝 搀 • 5ZQF4DSJQUכ㸜Ⰻ䚍锃眍〳腉 •
㸜Ⰻ䚍♴־ --noImplicitAnyך搀⸬⻉ anyas@ts-ignoreזו • 㸜Ⰻ䚍♳־ VOJPO㘗װ勴⟝㘗זוך崞欽 • 㸜Ⰻ䚍넝קו؝أز♳傻 㘗ػؤٕװֺׅה؝أػָ䝤⻉ -
㘗ך؝أزػؿؓ٦وٝأ 㸜Ⰻ䚍 㘗ךך 㷕统؝أز 涪؝أز 넝 搀 const items =
{ apple: { price: 128, }, orange: { price: 200, taxRate: 0.1 } } const getPriceIncludingTax = (name, taxRate) => { const item = items[name] taxRate = taxRate || item.taxRate if (taxRate == null) throw new Error("OMG") return item.price * taxRate } 㸜Ⰻ䚍搀 ! -
㘗ך؝أزػؿؓ٦وٝأ 㸜Ⰻ䚍 㘗ךך 㷕统؝أز 涪؝أز 넝 搀 const items: Record<string,
{ price: number; taxRate?: number }> = { apple: { price: 128 }, orange: { price: 200, taxRate: 1.1 } } const getPriceIncludingTax = (name: string, taxRate?: number) => { const item = items[name] taxRate = taxRate || item.taxRate if (taxRate == null) throw new Error("OMG") return item.price * taxRate } 㸜Ⰻ䚍⚥ ! ! -
㘗ך؝أزػؿؓ٦وٝأ 㸜Ⰻ䚍 㘗ךך 㷕统؝أز 涪؝أز 넝 搀 type ItemName =
'apple' | 'orange' const assertIsItemPriceData = <D extends Record<ItemName, { price: number; taxRate?: number }>>(data: D) => data const items = assertIsItemPriceData({ apple: { price: 128 }, orange: { price: 200, taxRate: 1.1 } }) type Distribute<N extends ItemName> = N extends ItemName ? ((typeof items)[N] extends { taxRate: number } ? (name: N) => number : (name: N, taxRate: number) => number) : never type UtoI<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never type getPriceIncludingTaxFunc = UtoI<Distribute<ItemName>> const getPriceIncludingTax: getPriceIncludingTaxFunc = ( name: ItemName, taxRate?: number ) => { const item = items[name] as any taxRate = (taxRate || item.taxRate) as number return item.price * taxRate } 㸜Ⰻ䚍넝 ! ! -
5ZQF4DSJQUך㸜Ⰻ䚍ה؝أز 㸜Ⰻ䚍 㘗ךך 㷕统؝أز 涪؝أز 넝 搀 غٓٝأ罋ִ״ֲ • 㸜Ⰻ䚍ָוְֻ妜׃ְךַ
וך玎䏝ך⽬ꤹ䚍鏩㺁ׅךַ • ؝أزוְֻ䩪ִךַ • 剑穄湡垥כוֿזךַ 䖓ַ54㼪Ⰵׅ㜥さ -
BOZ㘗 ! BOZ㘗ך暴䗙 BOZ㘗ך⦼כ⟣䠐ךⵃ欽ָ〳腉 ⡦׃ג㘗ؒٓ٦ָ饯ֹזְ BOZ㘗ך⦼ַ䖤ⴽך⦼BOZ㘗䭯א فٗػذ؍،ؙإأ٥ꟼ侧ㄎן⳿׃ך鵤⦼זו ⚠ BOZ㘗ך⽬ꤹ䚍 כװ㶷㖈荈⡤ָ㎳
㎳ָ⠗厩ׅ BOZ寅厩 ˟֮ה㘗䞔㜠ָ#חזךד酡㸣⸬ֹתׇ -
BOZ寅厩☠ const obj: any = doSomeMagic(); const res = obj.hello.world();
const num: number = obj; const str: string = obj; function doSomeMagic(){ /* empty */ } BOZ㘗ך㢌侧PCK⡲䧭 BOZ㘗ך⦼כוֲ⢪ג ؒٓ٦חזזְ BOZ㘗侧⦼װ俑㶵ה׃ג 䪔גؒٓ٦חזזְ -
BOZ寅厩☠ const obj: any = doSomeMagic(); const res = obj.hello.world();
const num: number = obj; const str: string = obj; function doSomeMagic(){ /* empty */ } ֿךPCKג㢌侧כוז괏ח ⢪גؒٓ٦חזזְ׃ 侧⦼ד֮׃俑㶵ד֮ أ״XXXXXXXXXXXX 㣐㎳ 寅厩ׁ㢌侧BOZ寅厩彁 鵤⦼כBOZ㘗✳如寅厩 BOZך鄃㹱 儗䊴余䷼ -
BOZ寅厩☠ const obj: any = doSomeMagic(); const res = obj.hello.world();
const num: number = obj; const str: string = obj; function doSomeMagic(){ /* empty */ } 寅厩眔㔲 BOZ㘗ך㢌侧ָ֮ أ؝٦فⰋג BOZꅿ佝׃חׅה ꬊ䌢ח⽬ꤹ 寅厩ׁ㢌侧BOZ寅厩彁 ˟BOZכ鸬ꓲ涸חOP*NQMJDJU"OZؒٓ٦涪欰ׇׁ挿䝤 -
BTח״㘗ٍؗأزך⢽ const v = getNumberOrString() as string; console.log(v.slice(0, 10));
function getNumberOrString(): number | string { return Math.random() ? "foo" : 123; } number | string㘗ך䒭 string㘗חتٍؐٝؗأز -
BTח״㘗ٍؗأزך⢽ const v = getNumberOrString() as string; console.log(v.slice(0, 10));
function getNumberOrString(): number | string { return Math.random() ? "foo" : 123; } 鵤⦼OVNCFS]TUSJOHהַ 鎉גֽווְֲׇא TUSJOHד׃׳XXXXXXXX 㣐㎳ BTך鄃㹱㹋遤儗ؒٓ٦ -
BTך䕦갟眔㔲 const v = getNumberOrString() as string; console.log(v.slice(0, 10)); function
getNumberOrString(): number | string { return Math.random() ? "foo" : 123; } ㎳ָ㢌侧ח⥂㶷ׁ ךד⟃꣬Ⰻגָ ㎳ך䕦갟眔㔲ח -
BTח״㘗ٍؗأزך⢽ function getAnchorOfNode(node: HTMLElement): HTMLAnchorElement | null { let
current: HTMLElement | null = node; while (current) { if (current.tagName === "A") { return current as HTMLAnchorElement; } current = current.parentNode as HTMLElement | null; } return null; } -
BTח״㘗ٍؗأزך⢽ function getAnchorOfNode(node: HTMLElement): HTMLAnchorElement | null { let
current: HTMLElement | null = node; while (current) { if (current.tagName === "A") { return current as HTMLAnchorElement; } current = current.parentNode as HTMLElement | null; } return null; } current.parentNodeך㘗 Node & ParentNode | nullַ HTMLElement | nullח⥜姻 ؒحآ؛٦أꤐֽל姻׃ְ -
㢌侧ח㎳⥂㶷׃זְ function getAnchorOfNode(node: HTMLElement): HTMLAnchorElement | null { let current:
HTMLElement | null = node; while (current) { if (current.tagName === "A") { return current as HTMLAnchorElement; } current = current.parentNode as HTMLElement | null; } return null; } -
㢌侧ח㎳⥂㶷׃זְ function getAnchorOfNode(node: HTMLElement): HTMLAnchorElement | null { let current:
HTMLElement | null = node; while (current) { if (current.tagName === "A") { return current as HTMLAnchorElement; } current = current.parentNode as HTMLElement | null; } return null; } BTך⽬ꤹ䚍ך䕦갟眔㔲 ֿך㘗䓼ⵖָ姻׃ְֿה➂ָ⥂鏾ׅל0, ֿֿכ姻׃ְ -
ꟼ侧ך؎ٝة٦ؿؑ٦أד㎳אַזְ interface Foo { type: 'foo' } function isFoo(obj: any):
obj is Foo { return obj != null && obj.type === 'foo' } ♷ִ⡦ַך⦼objָ Foo㘗ַוֲַⴻ㹀ׅꟼ侧 -
ꟼ侧ך؎ٝة٦ؿؑ٦أד㎳אַזְ interface Foo { type: 'foo' } function isFoo(obj: any):
obj is Foo { return obj != null && obj.type === 'foo' } BOZך䕦갟眔㔲כꟼ侧ⰻⰋג ꟼ侧Ⰻ⡤ך姻׃ׁ➂ָ ⥂鏾ׅל0, ˟勴⟝㘗זוꟼ侧ך㘗ח⢪ֲ㜥さ㣐⡤ֿחז勴⟝㘗כ䱿锷דֹזְךד -
'VSUIFS3FBEJOH 侁⻌罏ך5ZQF4DSJQUˊ 2JJUB CZ!VIZP 㸜Ⰻ䚍ך嚊䙀䓼ְ鎉衝ד铡ֻ鎸✲ BOZװBT⟃㢩ך⽬ꤹ䚍《䪔גְתׅ ׀㸜䗰ְֻׁծֿךز٦ؙךⰻ㺁㸚לⵃ罏דׅ 5ZQF4DSJQUךABTTFSUTYJT5A㘗כוך״ֲח⽬ꤹזךַ ˊ
2JJUB CZ!VIZP 顑⟣眔㔲杞ֻׅהְֲ罋ִ倯ك٦أחِ٦ؠ٦㹀纏㘗鶢铂 ך⢪ְ倯罋㻊׃鎸✲