Tarion
Tarion

Reputation: 17174

Access object properties from typescript

Having a *.d.ts file with the following definition:

class StateManager {
    states(key:string): Phaser.State;

Can be accessed in two ways:

myStateManager.states[key]
myStateManager.states(key)

But only the first will actually work due to the JS definition of states:

this.states = {};

Is there a way to force the correct notation in typescript?

Upvotes: 0

Views: 2043

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221352

It sounds like you want an index signature here instead?

class StateManager {
    states: { [key: string]: Phaser.State };
}
var x = new StateManager();
var p: Phaser.State = x.states['hello']; // OK
var e = x.states('hello'); // Error

Upvotes: 1

Related Questions