0

I am very new to typescript. Here is a problem I need to solve. I already have the following:

const enum MyEnum {
   ...
};
export class A {
  constructor(
    ...
    public enu?: MyEnu,
    public amount?: number,
  ) {
  }
}

Now, I need to move the above two attributes into a class B. After a few tries, I haven't been able to figure out a right way. The following is one of my tries:

export class A {
    constructor(
      ...
      public b?: B,
    ) {
   }
   export class B {
    constructor(
        public enu?: MyEnu,
        public amount?: number
    ) {
    }
}

What will be a right way to define an embedded class in this case?

Updated: I try the following:

export interface IB{} 
class B implements IB { 
  constructor(public enu?: MyEnu, public amount ?: number){} 
} 
export class A { 
  static B = B; 
  constructor(public b?: IB) { } 
}

Without a luck.

vic
  • 2,548
  • 9
  • 44
  • 74
  • 3
    `static Money = class { ... }`? But it's not obvious why you need a nested class, still. – zerkms Apr 03 '17 at 01:13
  • The TypeScript code mirror a Java code. The embedded structure is in the Java code. I just correct a typo. – vic Apr 03 '17 at 01:27
  • @Nitzan Tomer, the other question looks similar, but is not helpful in this case: you cannot type the class member with the new created nested class. Hence voted to reopen. – zerkms Apr 03 '17 at 01:44
  • @zerkms fair enough. reopened – Nitzan Tomer Apr 03 '17 at 01:51

1 Answers1

1

The only way I can think of, of pulling this off is something like this:

export enum MyEnum {
    ...
}

export interface IB {
    ...
}

class B implements IB {
    ...
}

export class A {
    static B = B;
    constructor(public b?: IB) {
        ...
    }
}

Or:

export class A {
    static B = (class B implements IB {
        ...        
    });
    constructor(public b?: IB) {
        ...
    }
}

You cannot reference A.B in the constructor signature.
Java and typescript are different, you cannot always "mirror" java code into typescript and this is one of those cases.

Also notice that the inner class is like a static inner class in java, you won't be able to access instance members of A from within A.B

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thanks Nitzan for your inputs. I try the following code: export interface IB{} class B implements IB { constructor(public enu?: MyEnu, public amount ?: number){} } export class A { static B = B; constructor(public b?: IB) { } } and my problem still exists. I shall say that the problem is in the content of Angular. Again, I am very new to TS. – vic Apr 03 '17 at 17:54
  • Code in comments (when longer than a line) isn't readable, so in the future edit your question, add the code there, and then update the person that you've added the code to your question. With this code you just posted, what exactly is the problem? Do you get errors? If so, what are they? – Nitzan Tomer Apr 03 '17 at 18:09
  • I don't see any error message on a browser debugging console, but a page is blocked out. Again, it is in an Angular front page application. – vic Apr 03 '17 at 18:21
  • You asked how to have an inner class in typescript, we've solved that. If it compiles then you're good to go. If things aren't working in your page then that's probably a completely different question. Your code shows nothing of angular for example. Create a new question with the relevant code in it and tag it with angular. – Nitzan Tomer Apr 03 '17 at 18:26