網站搜尋

為什麼 TypeScript 對返回超過明確 void 的箭頭函數可以接受,但對做同樣事情的函數卻報錯?


我找不到對以下 TypeScript 片段中發生的事情的解釋:

const f1: () => void = () => 4; // <- OK

function f2(): void {
  return 4; // <- Type 'number' is not assignable to type 'void'.
}

遊樂場連結

為什麼 TypeScript 對箭頭函數返回超過明確的 void 沒有問題,但對宣告函數做同樣的事情卻會抱怨?