Alexander Mills
Alexander Mills

Reputation: 99990

How do I give plain object an index signature

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.

Upvotes: 0

Views: 451

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249506

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
    }
}

Upvotes: 2

Related Questions