59

In my Angular 2 TypeScript application, I defined an interface rather than a class to allow optional parameters.

As far as I know, I should somewhere implement the interface by:

export class myClass implements myInterface { ... }

and then instantiate it via new(...).

I wondered whether this is the right way to do it (in Angular 2) or there is a simpler / better way?

Also, where should I put the implementation, in the component (.ts) where I use it, where the interface is or where?

6 Answers 6

102

You can do it that way. You can also just create an object that implements the interface like:

interface foo {
    one: number;
    two: string;
}

const bar: foo = { one: 5, two: "hello" };

If you want to use a class, you can put it where you want. If it's tightly coupled with the component, you can put it there. Generally though, I want classes to be loosely coupled, so I put them in their own file.

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

2 Comments

Another way is: interface foo { one?: number; two?: string; } const bar: foo = {}
Yet another way is const bar = { one: 5, two: "hello" } as foo which is slightly different, as it allows for additional properties beyond the ones defined by the interface. (I'm just noting this here for completeness - the bar: foo syntax is the correct thing unless you need additional properties, as it provides the strictest validation.)
72

I use this way

interface IObject{
    first: number;
    second: string;
}

then

var myObject = {} as IObject
var myListObject = [] as Array<IObject>

2 Comments

Thanks, this is a good solution for when I want to instantiate the variable with an interface, but don't know all of the fields at that point in time. It's much cleaner to implement for larger interfaces too.
This approach defeats the point of type safety: zohaib.me/creating-object-based-on-interface-type-in-typescript
18

While the accepted answer is good, beware of solutions like this because they allow you to omit required properties defined in the interface:

const instance1 = {} as MyInterface;
const instance2 = <MyInterface>{};

Some other robust and compact alternatives include:

1) Instantiate an anonymous class which implements the interface:

new class implements MyInterface {
  one = 'Hello';
  two = 'World';
}();

2) Alternatively, employ a utility function as follows:

export function impl<I>(i: I) { return i; }

impl<MyInterface>({
  one: 'Hello';
  two: 'World';
})

1 Comment

This should be the accepted answer, as it let me instantiate an interface without values.
10

You can do the following:

export interface IMyInterface {
  property1: string;
  property2: number;
  property3: boolean;
}

export class MyClass implements IMyInterface {
  property1: string = '';
  property2: number = 1;
  property3: boolean = false;
  constructor(){}
}

Then to instantiate:

let newFromInterface = new MyClass();

This is how I do it in my code at least.

1 Comment

then you don't need to define an interface at all, you are just using your class. Correct syntax is export class MyClass implements IMyInterface
3
interface IOffice {
id: number;
employeeName: string;
phone?: number;
}
export class OfficeComponent {
  officeData: IOffice= <IOffice>{};
  dummyOfficeData: IOffice = {id: 1, employeeName: 'ragnar', phone: 123};
  noPhoneOfficeData: IOffice = {id: 2, employeeName: 'floki'};

constructor(){
  console.log(this.dummyOfficeData);
  console.log(this.dummyOfficeData);
}
}

this is better way for me. you can export interface from model file and import in any necessary components.

Comments

1

If implementation is not done properly, you might end up with {} which is null but not actual null.

 interface foo {
    one: string;
    two: string;
  }

if you want to initialize a null interface use data: foo = null if you want to intialize interface with values use data: <foo>({one:'1',two:'2'})

1 Comment

The "data: <foo>({one:'1',two:'2'})" syntax is what I was looking for.

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.