Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill object JS inside new?

I have defination of object like:

let translateObj: ITranslate;

I tried to initializae this like corresponding interface:

let obj = new translateObj = {
          keyTranslate: 'subjectId',
          outName: 'name'
        }

It does not work...Can I use this suffix?

I tried this:

export class MapperServiceArray<T> implements IMapperServiceArray<T> {
  public constructor(public key: string | number, public translate?: ITranslate) {
}

I create instance like this:

 let mapperArray = new MapperServiceArray<ISubjectBase>('subjectId', {
          keyTranslate: 'subjectId',
          outName: 'name'
        });
like image 583
dooglu Avatar asked Apr 02 '18 08:04

dooglu


1 Answers

Why you use new translateObj ? If you declare an interface and want your object to be of type of that interface, you need just set the type to it and initialize an object without new.

let obj: ITranslate = {
    keyTranslate: 'subjectId',
    outName: 'name'
}
like image 163
Suren Srapyan Avatar answered Oct 01 '22 16:10

Suren Srapyan