Skip to main content
@spa-tools/utilities

asStrategy()

The asStrategy<P>() TypeScript utility takes a Record type and returns it with some special sauce as to enable intellisense on the respective record. Think of this as enabling your Record to behave like a static/instance class in the IDE but with neither the perf hit nor the verbose code.

This becomes useful if you have a complex object and you use it in many places, so gaining intellisense on the record's keys can really help with productivity.

The generic P defines the type (or unioned types) returned from the strategy Record values.

Usage

import { asStrategy } from '@spa-tools/utilities';

const asMyInterface = asStrategy<string | number>();

const myInterfaceObj = asMyInterface({
foo: 'hello',
bar: 42,
});

// now you can access tmyInterfaceObj and the IDE will
// magically provide intellisense for the keys
console.log(myInterfaceObj.foo);