簡體   English   中英

我收到錯誤 TS2345:X 類型的參數不可分配給 Y 類型的參數

[英]I have got error TS2345: Argument of type X is not assignable to parameter of type Y

我最近正在使用 angular 8。我是這個框架的新手。

我正在嘗試創建一個包含新元素的界面。 我奇怪地注意到前兩個字段我沒有問題。 相反,當我 go 插入新的時,會出現以下 linting 錯誤消息:

src/app/weatherObject/weather-class.ts(27,17) 中的錯誤:錯誤 TS2345:類型參數 '{ cityName: any; 度數:數字; 撞擊:數量; }' 不可分配給“WeatherFeature”類型的參數。 Object 文字只能指定已知屬性,並且“天氣特征”類型中不存在“影響”。

這是我的界面:

interface WeatherFeature {
    cityName: string,
    degrees: number,
    impaction: number //REFUSED FIELD
    // sky: string //READY TO BE PUT, But as long as impaction doesn't work I can't put it!
}

這是我分配值的 class 代碼:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { WeatherForecastApiService } from '../weatherForecastApiService/weather-forecast-api.service';

@Injectable({
    providedIn: 'root',
})
export class WeatherClass {

    public weatherFeature = new BehaviorSubject<WeatherFeature>(undefined);

    constructor(
        private wfas: WeatherForecastApiService,
    ) {
        this.retriver();
    }

    private retriver() {
        this.wfas.getItalyWeatherData('Pisa').subscribe((response) => {
            const ks: string[] = ['name', 'main', 'temp', 'pressure', 'weather'];
            console.log(response[ks[1]][ks[3]], response[ks[4]][0][ks[1]]);
            this.weatherFeature.next({
                cityName: response[ks[0]],
                degrees: Number((response[ks[1]][ks[2]] - 273.15).toFixed()),
                impaction: Number(response[ks[1]][ks[3]]) //WHY CAN I NOT PUT IT ???
            });
        });
    }
}

我不明白為什么,雖然我擴展了接口,但當我 go 在 class 文件的接口中插入新字段時,我收到了該錯誤消息。

我希望這是一個愚蠢的錯誤,我的誤解,僅此而已。

定義接口使用分號; 而不是逗號,來分隔屬性。

export interface WeatherFeature {
    cityName: string;
    degrees: number;
    impaction: number;
}

除非它在使用的同一個文件中,否則不要忘記導出/導入它。

您可能使用 WeatherFeature 編輯了錯誤的文件,maybe.bak

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM