skid skid - 4 years ago 157
TypeScript Question

Importing a interface in Angular2

I created a interface and tried to access it from an another class but i am getting the following error stating


'format'only refer's to a type but it is being used as a value here


my code looks like this

format.ts

export interface format{
firstName : String;
}


appcomponent.ts

import {format} from "./directory"
export class AppComponent{
private Name = format[];
}


I am getting this error when I assign the interface to the private object
Name
.

Answer Source

It should be

private Name: format[];

= it means you assign array

: means that you describe variable, it will be array of type format

Also you could do

private Name: format[] = [];

This means that you will have array of type format and when you will try to add anything to this array type script will only accept object which implements this interace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download