Ошибка TS2322 на TypeScript Тип "IGames []" не может быть назначен

Я хочу получить свои данные с Localhost на angular, используя Http и Observable, это моя служба

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { HttpParams } from '@angular/common/http';
import { IGames } from './games';

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

  constructor(private http: HttpClient) { }
  test():Observable<any>{
    return of("test");
  }
  getGames():Observable<IGames[]>{
    return this.http.get<IGames[]>('http://localhost/pmn/uas/getgames.php');
  }
}

Я создаю этот конструктор, чтобы получить его отовсюду

export interface IGames{id:number, name:string, description:string, pictute:string}

это мой games.component.ts

import { Component, OnInit } from '@angular/core';
import { GamesService } from '../games.service';
import { IGames } from '../games';

@Component({
  selector: 'app-games',
  templateUrl: './games.component.html',
  styleUrls: ['./games.component.css']
})
export class GamesComponent implements OnInit {

  constructor(private gs:GamesService) { }
  games=[];
  ngOnInit() {
    this.gs.getGames().subscribe(
      (data)=>{
        this.games=data;
        console.log=data;
      }
    );
  }
}

этот код работает, но на моем терминале он всегда показывает эту ошибку

ERROR in src/app/games/games.component.ts(18,9): error TS2322: Type 'IGames[]' is not assignable to type '(message?: any, ...optionalParams: any[]) => void'.
  Type 'IGames[]' provides no match for the signature '(message?: any, ...optionalParams: any[]): void'.

person Martin Christopher    schedule 16.12.2018    source источник
comment
работает для меня нормально, с двумя изменениями: 1) переместите объявление GamesComponent.games над конструктором и добавьте тип: games: IGames[] = []; 2) console.log=data должно быть console.log(data);   -  person muradm    schedule 16.12.2018
comment
Работает отлично спасибо   -  person Martin Christopher    schedule 16.12.2018


Ответы (1)


работает для меня нормально, с двумя изменениями: 1) переместите объявление GamesComponent.games над конструктором и добавьте тип: games: IGames[] = []; 2) console.log = data должно быть console.log(data);

export class GamesComponent implements OnInit {

  // members should be above constructor
  games: IGames = [];

  constructor(private gs:GamesService) { }
  ngOnInit() {
    this.gs.getGames().subscribe(
      (data)=>{
        this.games=data;
        // console.log is a function
        console.log(data);
      }
    );
  }
}
person muradm    schedule 16.12.2018
comment
У меня такая же ошибка, но я скопировал и вставил ее из этого ответа, но по-прежнему ошибка, в которой говорится, что Property 'includes' отсутствует в типе 'IGames'. - person Martin Christopher; 16.12.2018
comment
Для меня работает нормально, может быть, вы редактировали еще что-нибудь. Несколько раз необходимо также перезапустить ng serve. - person muradm; 16.12.2018