網站搜尋

typescript - 屬性 id 在型別 Location 上不存在


我收到一個看起來像這樣的編譯錯誤:

app/app.component.ts(69,24): error TS2339: Property 'id' does not exist on type 'Location'

然後我有一個函數 updateDiv() 看起來像這樣:

updateDiv(location: Location, infoWindow:SebmGoogleMapInfoWindow) {
   this.selectedLocation = location;
   this.ID = location.id;
   this.content = location.content;
 }

這裡是 HTML:

<sebm-google-map-overlay-view *ngFor="let location of locations" (markerClick)="updateDiv(location, infowindow)">

我該如何宣告 id 和 content 的屬性?

答案#1

屬性 'id' 在類型 'Location' 上不存在

當你說一個函數接受類型 Foo 時,TypeScript 會信任你(但你其實是想說它的 Bar)。如果你嘗試把它當作 Bar 使用,你會得到像這樣的錯誤。你大概不是想說 Location 🌹