There is a function
function createStats<K extends string[]>(arr: K): Stats<K[number]>
and when I do this for example
const testStats = createStats(["maxHealth"])
testStats is Stats<string> and I want it to be Stats<"maxHealth">
The only way I know how to fix it is to add as const. But I don’t want to write as const through out all of my code, so it isn’t really a good solution for me.
>Solution :
You can use a generic const-type parameter also known as const-modifier. Similar to a const-assertion TypeScript will try to infer the most specific type of a generic argument.
declare function createStats<const K extends readonly string[]>(
arr: K,
): Stats<K[number]>;
createStats(["maxHealth"]);
//^? createStats<readonly ["maxHealth"]>(arr: readonly ["maxHealth"]): "maxHealth"