寫法1 - 使用 function 關鍵字
function greeter(fn: (a: string) => void) {
fn("Hello, World");
}
function printToConsole(s: string) {
console.log(s);
greeter(printToConsole);
(a: string) => void
上述文法的含義:表示一個函數,接收一個字元串作為輸入參數,沒有傳回參數。
可以使用 type 關鍵字定義一個别名:
type GreetFunction = (a: string) => void;
1
Call signatures
使用 call signatures 給函數增加額外的屬性。TypeScript 的 function 也是 value,和其他 value 一樣。
注意:一定有 type 關鍵字。
源代碼:
