繁体   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