TypeScript の 宣言マージ
レベル 5352 打鍵
宣言マージは同じ名前の宣言を結合します。2つの Box インターフェースは width、height、depth を持つ1つにまとまり、Box 名前空間が同じ名前で create 関数を追加します。
TypeScript のコード
// Box の1つ目の宣言: width と height1interface Box {2 width: number;3 height: number;4}// 同じ名前をもう一度宣言し、depth が同じインターフェースにマージされる5interface Box {6 depth: number;7}// 同名の名前空間が Box に実行時の値を追加する8namespace Box { // 戻り値の型 Box は、マージ後のインターフェースの3つのプロパティすべてを必要とする9 export function create(w: number, h: number, d: number): Box {10 return { width: w, height: h, depth: d };11 }12}// ここでは Box を値(名前空間)として使う13const box = Box.create(10, 20, 30);// Width: 10、Height: 20、Depth: 30 をそれぞれ別の行に表示する14console.log(`Width: ${box.width}`);15console.log(`Height: ${box.height}`);16console.log(`Depth: ${box.depth}`);実行結果
Width: 10 Height: 20 Depth: 30
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この TypeScript のコードで打つ記号と、出てくる回数です。
:× 13;× 8{× 8}× 8.× 7,× 6`× 6(× 5)× 5$× 3