I have a problem which boils down to trying to assign a function with a specific parameter type to a variable expecting a function with a generic type:
const s: <T>(v: T)=>string
= (v: string[])=>(v[0]||'x');
Typescript gives the following error:
Type '(v: string[]) => string' is not assignable to type '(v: T) => string'. Types of parameters 'v' and 'v' are incompatible. Type 'T' is not assignable to type 'string[]'.
This error doesn't make much sense to me, as it seems like string[] is a perfectly reasonable type to use for the generic T.
There is a related question that has the same underlying problem, but the answer there was specific to circumstances in that question: Why is this TypeScript class implementing the interface not assignable to a generic constraint extending the interface?
<T>(v: T) => stringmeans the type of a function which can consume anything and output a string; your function can only consume an array of strings.const s: <T extends string[]>(v: T) => string = (v: string[]) => (v[0] || 'x');