網站搜尋

TypeScript:為什麼它會運作?


看起來當我們使用解構與動態鍵時,TypeScript 會出錯。

這裡是一段程式碼:

interface ISimple {
  field1: { value: string }
}

let o: ISimple;

const update = (key: string, value: string) => {
  o = { ...o, [key]: value }; // should throw error
}

我們看到 value 的類型不應該是 string(應該是 { value: string }),但 TypeScript 並沒有拋出錯誤?

TypeScript 版本:3.7.3