0

I have a base class like so

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

and a child classes setup like so

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

This is giving me the following error:

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

Not sure what am I missing here. Do I still need to implement getAll even if it is implemented in the base class ?

2
  • 1
    I think it should extend BaseObject<User>, not implement it. Commented May 6, 2016 at 17:52
  • Thanks that helped :) Commented May 6, 2016 at 18:01

2 Answers 2

1

The issue is more apparent if you remove things that might not be relevant to the error:

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

The problem is that you're using implements instead of extends. In TypeScript, a class definition produces type information (as if you had defined an interface), which is why it's allowing you to implement the class as if it were an interface.

You'll need to change that keyword. You'll also need to add a super() call to your constructor, and use public myFunction(strUserDetails: string): User { to define myFunction (you're currently defining it as an instance member).

Sign up to request clarification or add additional context in comments.

Comments

0

You should use extends to inherit classes. Do not forget to call super class constructor as the doc says:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.