0

In a TypeScript 2.2.2 project, I have the following configuration :

export const MAIN_CONFIG: AppConfig = {

    REST_API_BASE_ENDPOINT: 'api/v1',
    REST_API_SECURITY_ENDPOINT: this.REST_API_BASE_ENDPOINT + '/security/test',
}

I expected that at runtime REST_API_SECURITY_ENDPOINT value will be api/v1/security/test but I got undefined/security/test

Is there any way to initialize a const member with another ?

Thank you.

Radouane ROUFID
  • 10,595
  • 9
  • 42
  • 80
  • AFAIK "this" is in context of a class, here the class is AppConfig so the initialization parameters ```REST_API_BASE_ENDPOINT``` will not be available as ```this.REST_API_BASE_ENDPOINT``` until the instance has been initialized – Surya Pratap May 12 '17 at 15:04
  • @SuryaPratap: That's not a class, it's just an object initializer. – T.J. Crowder May 12 '17 at 15:05
  • @Crowder The answer you proposed does not work ! – Radouane ROUFID May 12 '17 at 15:08
  • @T.J.Crowder I am not very sure of how object initalizers work, but I am pretty sure that ```this``` is generally used to refer to a function context in javascript – Surya Pratap May 12 '17 at 15:11
  • @SuryaPratap: No, what `this` means depends on where it's used. At global scope, `this` is a reference to the global object. In a module, `this` is `undefined` (I think). In a non-arrow function, it's whatever it was set to when that function was called. – T.J. Crowder May 12 '17 at 15:24

0 Answers0