Xenos
Xenos

Reputation: 71

Define in typescript object type to contain a specific property key

I'm working on a Ionic Sqlite database helper library and to ease the work on the rest of the functions, I would need to require the user to have the ID key on the Params property along side his other keys.

   interface dbObj {
      tableName: string;
      orderBy: string;
      Params: paramsList;
    }

The Params property should require to have the ID key in it, but should not interfere with any other keys the user might add after in the Params.

interface paramsList {
    ID: string;
    ????
}

How do I enforce having the ID key in Params but let the user freely add any other key in paramsList? I hope this does not sound stupid since I'm still new to typescript.

Upvotes: 7

Views: 3413

Answers (1)

Артур Гудиев
Артур Гудиев

Reputation: 1144

This should work.

   interface dbObj {
      ...
      Params: {ID: string, [key: string]: any};
    }

Now Params is forced to have ID and it also can contain any other field.

Upvotes: 2

Related Questions