Given the following declaration:
declare function useStrings<T extends string[]>(
arr: T,
fn: (item: T extends Array<infer U> ? U : never) => void
): void;
I expect the item parameter in fn to be a union type of the values of T.
However, when invoked like this, item is typed as a string:
useStrings(["a", "b", "c"], (item) => {});
// ^^^^ string
The following example gives the desired result, but is of course not a desirable solution:
useStrings(["a", "b", "c"] as ("a" | "b" | "c")[], (item) => {});
// ^^^^ "a" | "b" | "c"
How to get item inferred as a union without resorting to hacky assertions such as in the last example?
