2016-05-06 8 views
0

を認識していない私はそう活字体 - ベースクラスで非抽象メソッド

export abstract class BaseObject<TResult>{ 
    abstract myFunction(values: string): TResult; 
    getAll(param1: string): Promise<Array<TResult>> { 
     // do something 
    } 
} 

のような基底クラスとこれは私に次のエラーを与えているので、

export interface IUser { 
    userID: number; 
    firstName: string; 
    lastName: string; 
} 

export class User implements BaseObject<User>, IUser { 

    constructor(
     public userID: number, 
     public firstName: string, 
     public lastName: string 
    } 

    myFunction= (strUserDetails: string): User => { 
     // do something 
    } 
} 

のような子クラスを設定しています。

Class 'User' incorrectly implements interface 'BaseObject<User>'. Property 'getAll' is missing in type 'User'. 

私はここで何が欠けているのか分かりません。基本クラスに実装されていてもgetAllを実装する必要がありますか?

abstract class BaseObject{ 
    public getAll(): void {} 
} 
class User implements BaseObject { 
} 

問題は、あなたの代わりにextendsimplementsを使用していることである:あなたがエラーに関連しないかもしれないものを削除する場合

+1

私は 'それをimplement'ない、それは' 'BaseObject を延長すべきだと思う:docが言うように、スーパークラスのコンストラクタを呼び出すことを忘れないでください。 –

+0

ありがとうございました:) – antiskidwarpdrive

答えて

1

問題はより明白です。 TypeScriptでは、クラス定義は(インターフェイスを定義した場合のように)型情報を生成します。そのため、クラスをインターフェイスのように実装することができます。

このキーワードを変更する必要があります。 super()コールをコンストラクタに追加し、public myFunction(strUserDetails: string): User {を使用してmyFunction(現在はインスタンスメンバーとして定義しています)を定義する必要があります。

0

クラスを継承するには、extendsを使用する必要があります。

Derived classes that contain constructor functions must call super() which will execute the constructor function on the base class.

export abstract class BaseObject<TResult>{ 
    abstract myFunction(values: string): TResult; 
    getAll(param1: string): Promise<Array<TResult>> { 
    // do something 
    } 
} 

export interface IUser { 
    userID: number; 
    firstName: string; 
    lastName: string; 
} 

export class User extends BaseObject<User> implements IUser { 
    //Fixed your syntax errors here. 
    constructor(
    public userID: number, 
    public firstName: string, 
    public lastName: string) { 
    super(); 
    } 

    //Keep your syntax, I use this syntax to keep the method syntax consistent 
    myFunction(strUserDetails: string): User{ 
    // do something 
    } 
}