Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Typescript5.4の新機能
Search
rakus frontend
April 21, 2024
0
300
Typescript5.4の新機能
rakus frontend
April 21, 2024
Tweet
Share
More Decks by rakus frontend
See All by rakus frontend
新卒FEが1年目に取り組んだこと・学んだこと
rakus_fe
0
48
ErrorBoundaryとSuspenseの導入検討
rakus_fe
1
750
日付をもう少し真面目に勉強中
rakus_fe
0
43
React19 β をざっと見る
rakus_fe
0
320
Reactのパフォーマンス改善例
rakus_fe
0
510
非破壊的な配列メソッド
rakus_fe
0
360
ココがすごいぜ!Playwright Component Test
rakus_fe
0
540
スプレッドシートのセル結合がつらいので足掻いてみた話
rakus_fe
0
250
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
369
20k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Being A Developer After 40
akosma
91
590k
GraphQLとの向き合い方2022年版
quramy
49
14k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
A better future with KSS
kneath
239
18k
How to Ace a Technical Interview
jacobian
280
24k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
Leading Effective Engineering Teams in the AI Era
addyosmani
7
640
Transcript
TypeScript5.4の新機能 村山陽亮
型の絞り込み 5.4以前 function getUrls(url: string | URL, names: string[]) {
if (typeof url === "string") { url = new URL(url); } return names.map(name => { // if (typeof url === "string") return; url.searchParams.set("name", name); return url.toString(); }); } 関数クロージャ内で型情報が 保持されなかった 関数内でもう一度型情報 を絞り込む必要がなくなっ た
型の絞り込み 注意点 function printValueLater(value: string | undefined) { // ここで呼び出すことが可能
// toUpperCaseFn(); if (value === undefined) { value = "missing!"; } function toUpperCaseFn() { // エラーが発生する console.log(value.toUpperCase()); } // エラーは発生しない const toUpperCaseFn2 = () => console.log(value.toUpperCase()); } functionで関数宣言をした場 合、ホイスティングができるた め、型情報は保持されない
NoInfer ・5.4から新しく追加されたユーティリティ型 ・不要な型推論をブロックすることができる function createStreetLight<C extends string>(colors: C[], defaultColor?: C)
{ // ... } // blueはcolorsの配列に含まれていないのにエラーにならない createStreetLight(["red", "yellow", "green"], "blue"); // defaultColorからCが推論されるのを防ぐ function createStreetLight<C extends string>(colors: C[], defaultColor?: NoInfer<C>) { // ... }
まとめ ・NoInfer→いまいち使い所が思いつかなかった… ・型の絞り込み→役立つが場面ができてきそうだと思った