0

In an Angular project, I am using an interface.

    export interface School {
      name: string = '';
      website: string;
      registrationNumber: string;
      dateEstablished: Date;
      address: string;
      country: [];
      companyLogo: any = '';
    
        clear() {
          this.name = '';
          this.website = '';
          this.registrationNumber = '';
          this.dateEstablished = null;
          this.address = '';
          this.country = [];
          this.companyLogo = '';
      }
    }

I got two errors:

  1. Object is possibly 'undefined' - and all the "this." highlighted

  2. Declaration or statement expected.ts(1128) - and the last closing curly brace highlighted.

  3. expected call-signature: 'clear' to have a typedef (typedef)tslint(typedef)

How do I get these resolve?

Thanks

2
  • 4
    I could be mistaken, but I don't think an interface can contain a method implementation. Interfaces describe the members that should be present in the concrete implementation of the interface. They don't contain their definitions. Have you seen information that suggests otherwise? Commented Jun 6, 2021 at 19:32
  • @JLRishe - What's the best way to go about it. Kindly advise Commented Jun 6, 2021 at 19:38

1 Answer 1

1

You need an implementation of that interface like this:

file: school.ts

interface ISchool {
      name: string = '';
      website: string;
      registrationNumber: string;
      dateEstablished: Date;
      address: string;
      country: [];
      companyLogo: any = '';
    
      clear(): () => void; 

    }


export class School implements ISchool {

      // Declare all attributes here;
      public name: string = '';
      ...

      clear() {
          this.name = '';
          this.website = '';
          this.registrationNumber = '';
          this.dateEstablished = null;
          this.address = '';
          this.country = [];
          this.companyLogo = '';
      }

}

file app.ts

import {School} from 'school.ts';

const someSchool = new School();
someSchool.clear();

This is a basic object orientation technic so i would recommend to look up the use of classes together with interfaces.

You dont need the interface if you only have one type of school though.

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

3 Comments

Which of the two (2) exports am I using? class or interface?
You have to instantiate an object of the class school like const someSchool = new School(); and then call someSchool.clear();
Sorry, I don't understand. Can you update the code?

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.