0

I have this object:

const events = {
  i: 'insert',
  u: 'update',
  d: 'delete'
};

for some reason I am blanking on how to give the object an index signature - if I do this:

export interface EventsSignature {
  [key:string]: string
}

const events = <EventsSignature>{
  i: 'insert',
  u: 'update',
  d: 'delete'
};

that doesn't work, just overrides the object definition. Note I have the same problem when doing this:

export class OplogObservable {

  private uri: string;
  private coll: Collection;
  collName: string;
  isTailing = false;

  private subs = {
    all: new Subject<any>(),
    update: new Subject<Object>(),
    insert: new Subject<Object>(),
    delete: new Subject<Object>(),
    errors: new Subject<Object>(),
    end: new Subject<Object>()
  };

}

if I do new OplogObservable().subs[type] it will complain saying there is no index signature.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

2

The object literal already has an index signature, it's just that you can't index by an arbitrary string, it needs to be a key of the type:

export class OplogObservable {

    private subs = {
        all: new Subject<any>(),
        update: new Subject<Object>(),
        insert: new Subject<Object>(),
        delete: new Subject<Object>(),
        errors: new Subject<Object>(),
        end: new Subject<Object>()
    };
    test(type: string) {
        this.subs['all'] // ok using a constant 
        let subs = this.subs;
        this.subs[type as keyof typeof subs] // ok if we use a type assertion on the key
    }
}

If you really want to index using an arbitrary string you could use a type assertion to any:

 (this.subs as any)[type]

Or use a helper function to create an object with both the properties and the indexer:

function eventsHelper<T extends { [name: string] :Subject<any> }>(subs: T) : T & { [name: string] :Subject<any> }{
    return subs;
}
export class OplogObservable {

    private subs = eventsHelper({
        all: new Subject<any>(),
        update: new Subject<Object>(),
        insert: new Subject<Object>(),
        delete: new Subject<Object>(),
        errors: new Subject<Object>(),
        end: new Subject<Object>()
    });
    test(type: string) {
        this.subs.all // props preserved
        this.subs[type] // ok if we use a simple string to index
    }
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357