Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript / Angular 2: Property is missing in type

I'm still quite new to Typescript and have trouble wrapping my head around interfaces. As I understand an interface is a type description or a contract for a class. It allows me to define, which properties a class can have and of which type they are. I try to implement that, but always get this error:

error TS2420: Class 'ResultPage' incorrectly implements interface 'ResultPageInterface'.
Property 'entries' is missing in type 'ResultPage'.

Here is the code for the interface

export interface ResultPageInterface {
  entries:Array<any>;
  page_number:number;
  page_size:number;
  total_entries:number;
}

export class ResultPage implements ResultPageInterface {}

And here the class where I want to use it.

import { Injectable } from '@angular/core';
import { ResultPage } from '../interfaces/result-page.interface';

@Injectable()
export class SomeClass {

  constructor() {}

  buildPage(res:any): ResultPage {
    let page:ResultPage = new ResultPage();
    page.entries = res.data;
    page.page_number = res.pagination.page_number;
    page.page_size = res.pagination.page_size;
    page.total_entries = res.pagination.total_entries;
    return page;
  }

}
like image 636
Ole Spaarmann Avatar asked Nov 02 '16 10:11

Ole Spaarmann


3 Answers

Just in case if you do silly mistakes. I was getting the same error today.

ERROR in src/app/components/home/home.component.ts(11,3): error TS2322: Type 'undefined[]' is not assignable to type 'StreamData'.
Property 'messageId' is missing in type 'undefined[]'. src/app/components/home/home.component.ts(18,24): error TS2339: Property 'push' does not exist on type 'StreamData'.

I changed my code from dashboardData: StreamData = []; to dashboardData: StreamData[] = [];, that fixed the issue.

like image 199
Sibeesh Venu Avatar answered Oct 11 '22 13:10

Sibeesh Venu


You need to put the members in the class as well so that it will indeed implement the interface:

class ResultPage implements ResultPageInterface {
    public entries: Array<any>;
    public page_number: number;
    public page_size: number;
    public total_entries: number;

    constructor() {
        this.entries = [];
    }
}

You can also have the members private and use getters:

class ResultPage implements ResultPageInterface {
    private _entries: Array<any>;
    private _page_number: number;
    private _page_size: number;
    private _total_entries: number;

    constructor() {
        this._entries = [];
    }

    public get entries() {
        return this._entries;
    }

    public get page_number() {
        return this._page_number;
    }

    public get page_size() {
        return this._page_size;
    }

    public get total_entries() {
        return this._total_entries;
    }
}
like image 33
Nitzan Tomer Avatar answered Oct 11 '22 13:10

Nitzan Tomer


implements ResultPageInterface you state that ResultPage implements ResultPageInterface but it actually doesn't.

It should be

export class ResultPage implements ResultPageInterface {
  entries:Array<any>;
  page_number:number;
  page_size:number;
  total_entries:number;
}
like image 41
Günter Zöchbauer Avatar answered Oct 11 '22 12:10

Günter Zöchbauer