TypeScript
/icons/hr.icon
ドキュメント
TypeScript's Type System
TypeScript 日本語ハンドブック | js STUDIO
Documentation · TypeScript (英語)
typescript cheatsheet
詳しく: TypeScriptの型入門
TypeScriptの型メモ - Qiita
npm module に typescript の型定義がない時に、とりあえずビルドが通るようにする
unipota/tsundoku Private v-lazy-image.d.ts を修正
TypeScript 2.0 Non-null assertion operator
実行
Taskを使う場合 (Taskfile.yaml)
code: (yaml)
version: '2'
tasks:
default:
cmds:
- 'tsc main.ts'
sources:
- main.ts
文法
interface, type
TypeScript interfaceの拡張あれこれ
TypeScriptのInterfaceとTypeの比較 - Qiita
インターフェース
インターフェースの拡張
インデックス可能な型
code:typescript
let ret: {
index: string: boolean
} = {}
dtsgenerator - npm
swagger.yaml から d.ts ファイルを生成
code: (typescript)
type IndexType = 'hoge' | 'fuga'
interface MyObject {
key in IndexType: string
}
これはわかりにくいので非推奨 (Record <IndexType, string> にしたほうがいい)
Utility Types
Utility Types · TypeScript (英語)
TypeScript特有の組み込み型関数 - log.pocka.io
Partial<T>
型Tのプロパティをすべてoptionalにした型を定義する
code: (typescript)
interface Todo {
title: string;
description: string;
}
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}
const todo1 = {
// Todo 型
title: 'organize desk',
description: 'clear clutter',
};
const todo2 = updateTodo(todo1, {
// Partial<Todo> 型
// title がなくてもエラーにならない
description: 'throw out trash',
});
Required<T>
Tの型をすべてrequiredにした新しい型を定義する
Readonly<T>
Tの型をすべてreadonlyにした新しい型を定義する
Record<K, T>
型TなプロパティKを持つレコード型
code: (typescript)
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const x: Record<Page, PageInfo> = {
about: { title: 'about' },
contact: { title: 'contact' },
home: { title: 'home' },
};
TypeScript の型システム
TypeScriptの型メモ - Qiita
型 A の変数 a 、型 B の変数 b があるとき、 a = b が well-defined ならば A は B を部分型に持つ
「A が B を部分型に持つ」とは: 型の互換性 - TypeScript Deep Dive 日本語版
型変数の変性がある
TypeScript における変性(variance)について - 30歳からのプログラミング
Structual (構造的) SubTyping を採用している
一部例外がある
過剰プロパティのチェック
TypeScriptのunion型はorです 〜union型、構造的部分型、余剰プロパティチェックの話〜 - Qiita
Nominal (公称的) に型を区別したい場合
例: TypeScriptでradianとdegreeを間違えないようにする | Kabuku Developers Blog
code: (typescript)
type Degree = number
type Radian = number
↑のように定義された2つの型があったとき、 Degree のみを変数として受け取れる関数を定義したい……したくない? という話
やり方は色々ある
Nominal Typing - TypeScript Deep Dive 日本語版
Nominal typing techniques in TypeScript - Michal Zalecki
private property を持つClass
brand を property として持つ interface
一番素直なやり方、TypeScript Deep Dive でも推奨されている
code: (typescript)
interface Degree = {
__brand: 'Degree'
value: number
}
変数 x: Degree の値がほしいとき、 x.value のように取り出す必要があるのがポ
intersection type を使う
code: (typescript)
type Degree = number & { __brand: 'Degree' }
type Radian = number & { __brand: 'Radian' }
const x = 180 as Degree
const y = Math.PI as Radian
const someFunc = (val: Degree) => val + 2
console.log(someFunc(x)) // ok
console.log(someFunc(y)) // error
x: Degree の値がほしいときに、x でそのまま取り出せるので嬉しい
これをさらに汎用化するとこう: TypeScript で幽霊型っぽいものをつくる
AST
既存コードをいろいろいじりたいとき
ts-morph
ts-morph/packages/ts-morph at latest · dsherret/ts-morph
ts-morphを使ってTypeScriptプロジェクトのリファクタ・AST操作をお手軽にやる - Qiita