1

I have some config modules which export a const object literal, like so:

export const config = {
  foo: 'foo',
  bar: 'bar',
} as const;

I want to make widened copies for testing purposes. Something like this:

export const config = {
  foo: 'foo',
  bar: 'bar',
} as const;

export type Lookalike<T> = {
  [K in keyof T]: extends T[K]; // not possible
};

export const bar: Lookalike<typeof config> = {
  foo: 'dab',
  bar: 'bab',
};

However this is illegal as foo must the the string literal 'foo'.

I don't want to have to create types manually eg type Config = {foo: string; ...etc} as in my real use, they are much more complex and subject to change.

Is there a way to make a lookalike/widened type based off a const literal type?

old greg
  • 799
  • 8
  • 19

0 Answers0