1

I need to initialize initial value of 'gDetailDS' object. I cannot create classes instead of interfaces and interfaces don't allow me to set default values.

I have tried using map or other functions to resolve the issue but could not get success. Using map, get the error "_this.gDetailDS.map is not a function".

export interface InterfaceA{
    sysSeq: number;
    code: string;
    isUpdate: boolean;
    version: number;
}

export interface InterfaceB{
    sysSeq: number;
    name: string;
    isReserved: boolean;
    isPublic: boolean;
    version: number;
 }


export class DetailComponent<T> extends CoreComponent {
    gDetailDS: any;
    constructor(){
        this.gDetailDS =  <T> new Object; 
    }

}

I want to get default values specially for booleans. for instance, after executing this below line,

this.gDetailDS =  <InterfaceB> new Object; 

I should have an object of type InterfaceB with default sets for

this.gDetailDS.isReserved has default value "false";
this.gDetailDS.isPublic  has default value "false";

Your help would be much appreciated.

1 Answer 1

1

Once transpiled to Javascript, your DetailComponent<T> has no information about the T. So you can not use it directly to get an instance of T.

But, if you can add parameters to your DetailComponent constructor, a solution to your problem would be:

export class DetailComponent<T> extends CoreComponent {
  gDetailDS: T;
  constructor(newInstance: new () => T){
      this.gDetailDS = new newInstance(); 
  }

}

class InterfaceAImpl implements InterfaceA {
  sysSeq: number;
  code: string;
  isUpdate: boolean;
  version: number;
};

var detailComponent: DetailComponent<InterfaceA> = new DetailComponent<InterfaceA>(InterfaceAImpl);
Sign up to request clarification or add additional context in comments.

4 Comments

How could I set default value for isUpdate? Another point is that, I dont want to create a class for an interface.
Like types, interfaces have no existance once the code is transpiled into javascript: Playground
Thats I know buddy, interface does not exist after transpilation. But before transpiling, how could we initialize our interface value.
You can maybe have a look at the issue below: How to use interface and default parameters together? I'm not sure we can achieve having default values for interfaces properties without using classes directly (a class that implements the interface) or indirectly (like using a factory that build objects that are the type of your interface).

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.