doublemc
doublemc

Reputation: 3301

Get property from {} object

I'm using library which has a method loadUserInfo(): KeycloakPromise<{}, void> . I'm calling it using `this.getKeycloakInstance().loadUserInfo().success(data => { this.user.id = (data).sub;

I checked using debugger that this data object has field sub which I want to assign to this.user.id I cannot do so becasue I'm getting compilation error: TS2339: Property 'sub' does not exist on type '{}'.

I cannot change return type of method loadUserInfo() as it's external lib which I have to use. Can I get this sub property somehow without error?

Upvotes: 0

Views: 96

Answers (1)

user4676340
user4676340

Reputation:

You can with variable['sub'], or by typing your variable as any.

this.getKeycloakInstance()
  .loadUserInfo()
  .success(data => this.user.id = data.sub); // error

this.getKeycloakInstance()
  .loadUserInfo()
  .success(data => this.user.id = data['sub']); // no error

this.getKeycloakInstance()
  .loadUserInfo()
  .success(data => this.user.id = (data as any).sub); // no error

this.getKeycloakInstance()
  .loadUserInfo()
  .success((data: any) => this.user.id = data.sub); // no error

Upvotes: 3

Related Questions