Can you create types with dynamic literals?

Say I have this function:
const createType = <T extends string>(code: T) => {
  const literal: string = code;
  return type({
    code: `'${literal}'`
  });
};

With this I'm trying to create types with literals but I don't know those literals until runtime.

If I run:
const TypeONE = createType("ONE");

It creates type:
const TypeONE: Type<{
    code: string;
}, {}>

But instead what I'm actually wanting is:
const TypeONE = type({
  code: "'ONE'"
});

Which produces the correct type:
const TypeONE: Type<{
    code: "ONE";
}, {}>
Was this page helpful?