简体   繁体   中英

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

I am recently working with angular 8. I'm a novice with this framework.

I'm trying to create an interface with new elements. I strangely notice that with the first two fields I have no problem. Instead when I go to insert new ones the following linting error message appears:

ERROR in src/app/weatherObject/weather-class.ts(27,17): error TS2345: Argument of type '{ cityName: any; degrees: number; impaction: number; }' is not assignable to parameter of type 'WeatherFeature'. Object literal may only specify known properties, and 'impaction' does not exist in type 'WeatherFeature'.

This is my interface:

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!
}

And this is the class code on which I assign the values:

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 ???
            });
        });
    }
}

I can't understand why, although I expand the interface, I get that error message when I go to insert the new fields in the interface in the class file.

I hope it's a stupid mistake, a misunderstanding of mine, and nothing more.

Defining an interface use semicolons ; instead of commas , to separate properties.

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

Unless it's in the same file where used, do not forget to export/import it.

You probably edit wrong file with WeatherFeature, maybe.bak

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM