1

I am trying to write up an interface class which also contains other classes but i am getting an error when i do so but not sure what is wrong:

Interface:

export interface Languages{
   static English = class{
        id: number,
        section:number,
        name: string
   },
   static Chinese = class{
        id: number,
        section: number,
        name: string
   }
}

Both static and English are showing errors stating: [ts] Property or signature expected. [ts] Cannot find name 'English'.

1
  • 2
    There's no static in an interface. An interface describes how the instance looks like. If you want to describe static members/methods you need to create a different interface to represent that, like you have with Array and ArrayConstructor for example Commented May 8, 2017 at 20:46

1 Answer 1

1

Interfaces is a contract, not an implementation. Your English and Chinese members are instances which is exactly what an interface cannot support. They are also static, which is also incorrect on an interface. You could refactor it like this:

export interface Languages{
   English: Language;
   Chinese: Language;
}

export interface Language {
    id: number;
    section:number;
    name: string;
}

A better design would be to use a dictionary or a list with a lookup especially if this is a non-fixed size list where languages may be added later. Something like this for example:

export interface Languages{
   getLanguageByCode(isoCode:string): Language;
   getLanguageById(id: number): Language;
   allLanguages: Language[];
}
Sign up to request clarification or add additional context in comments.

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.