TypeScript の OmitThisParameter ユーティリティ型
レベル 4264 打鍵
OmitThisParameter ユーティリティ型は関数型からthisパラメータを除去します。
TypeScript のコード
// 最初のパラメータ this: { name: string } はthisの型指定で、呼び出し側はgreetingだけを渡す1function greet(this: { name: string }, greeting: string): string { // greetingとthisのnameを組み合わせて返す2 return `${greeting}, ${this.name}!`;3}// OmitThisParameter でthisパラメータを除いた (greeting: string) => string を得る4type GreetWithoutThis = OmitThisParameter<typeof greet>;// bindでthisを { name: "Alice" } に固定するので、結果の関数にthisは不要5const boundGreet: GreetWithoutThis = greet.bind({ name: "Alice" });// 普通の関数として呼び、「Hello, Alice!」と表示する6console.log(boundGreet("Hello"));実行結果
Hello, Alice!
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この TypeScript のコードで打つ記号と、出てくる回数です。
:× 6{× 5}× 5;× 4"× 4(× 4)× 4.× 3,× 2`× 2