TypeScript
TypeScript: JavaScript With Syntax For Types.
TypeScript: TS Playground - An online editor for exploring TypeScript and JavaScript
TypeScript
由Microsoft開發的JavaScript完全超集語言
替JavaScript追加了如型別定義等功能
最終仍會編譯成JavaScript
會在編譯時進行檢查,藉此提昇程式品質
例如呼叫函式或帶入時的型別是否正確
會盡早加入ECMAScript的最新功能,提昇對於瀏覽器的支援度
通常使用.ts副檔名
會將.ts檔編譯為.js檔
如React或Angular也經常利用TypeScript
React的副檔名則為.tsx
2023-03-03 温故知新 TypeScript10年の歴史を振り返る - Speaker Deck
2024-10-07 TypeScriptは型安全じゃないからすばらしい - まめめも
TypeScript/型別宣告
TypeScript/型別
TypeScript/型別推論
安裝
$ npm install -g typescript
$ tsc hello.ts
Node.js
$ npm install -g ts-node typescript
修改 package.json
"scripts":{ "ts-node": "ts-node" },
$ npm run ts-node
Learn TypeScript - Free Interactive TypeScript Tutorial
TypeScript 新手指南
TypeScript入門『サバイバルTypeScript』〜実務で使うなら最低限ここだけはおさえておきたいこと〜
TypeScript 教程 - 网道
type-challenges
type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
Re: type-challengesから始めるTypeScript実践演習 初級〜中級編
仕事ですぐに使える TypeScript
2022-12-18 実務経験ほぼゼロでTypeScriptやNext.jsを扱う開発案件にアサインされた人のための備忘録
jkchao/typescript-book-chinese: TypeScript Deep Dive 中文版
TypeScript Deep Dive 日本語版について
2023-06-13 TypeScript 本体のコードを読んでみよう
code:typescript
class InvalidDateFormatError extends Error {}
class DateIsInTheFutureError extends Error {}
function parse(birthday: string): Date {
let date = new Date(birthday)
if (isNaN(date.getTime())) {
throw new InvalidDateFormatError("Enter a date in the form YYYY/MM/DD");
}
if (date.getTime() > Date.now()) {
throw new DateIsInTheFutureError("Are you a timelord?");
}
return date;
}
try {
const date = parse("2025/01/01");
console.log("Date is", date.toISOString());
} catch (e) {
if (e instanceof InvalidDateFormatError) {
console.error(e.message)
} else if (e instanceof DateIsInTheFutureError) {
console.warn(e.message)
} else {
throw e;
}
}
code:typescript
class InvalidDateFormatError extends Error {}
class DateIsInTheFutureError extends Error {}
function parse(
birthday: string
): Date | InvalidDateFormatError | DateIsInTheFutureError {
let date = new Date(birthday)
if (isNaN(date.getTime())) {
throw new InvalidDateFormatError("Enter a date in the form YYYY/MM/DD");
}
if (date.getTime() > Date.now()) {
throw new DateIsInTheFutureError("Are you a timelord?");
}
return date;
}
const result = parse("2025/01/01");
if (result instanceof InvalidDateFormatError) {
console.error(e.message)
} else if (result instanceof DateIsInTheFutureError) {
console.warn(e.message)
} else {
console.log("Date is", result.toISOString());
}