Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to enable IntelliSense and indexer at the same time in typescript?

For example, I have an object:

const obj = {
  a: (valueA: string) => true;
  b: (valueB: string) => false;
  c: (valueC: string) => false;
};

You see all properties in this object have same type ((value: string) => boolean). Now I want it to have IntelliSense which makes follows possible:

  1. Display a, b, c after I typed obj.;
  2. Limit its type when I try to add a property into obj.

I tried indexer:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const obj: Record<string, ((value: string) => boolean)> = {
  a: (valueA: string) => true;
  b: (valueB: string) => false;
  c: (valueC: string) => false;
};

But now target 1 won’t happen, if I don’t use indexer, I cannot achieve target 2.

>Solution :

Variable inference is either all or nothing, you either let typescript infer the type or you specify the type yourself.

You can only do this with a helper function:

function makeFunctionsMap<T extends Record<string, ((value: string) => boolean)>>(o: T) { return o; }
const obj = makeFunctionsMap({
  a: (valueA: string) => true,
  b: (valueB: string) => false,
  c: (valueC: string) => false,
})
obj.a;
obj.d // Error

Playground Link

You can also inline the function if you want to make it shorter and more unreadable (Playground Link)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading