My configuration object looks like this:
const config = { envs: ['dev', 'test', 'prod'], targets: ['> 2%'] };
Currently, the TypeScript compiler interprets the type of this object as:
type IConfig = { envs: string[], targets: string[] };
This makes sense because I might modify these arrays after declaration.
However, I don't plan on changing them, so I want the type to be:
type IConfig = { envs: ['dev', 'test', 'prod'], targets: ['> 2%'] };
Is there a way to instruct the compiler to infer the type of config.envs
as a tuple type with string literals without explicitly typing it out?
Edit: The top answer is almost there, but I'm looking for a solution that can be applied to the entire object rather than each individual property. To clarify, I have added another property to the examples.