I'm trying to properly type a function that takes an argument and wraps it into an array, however, I can't get the typings to work properly.
function wrapInArray<T>(data:T):T extends Array<infer K>? K[]: T[]{
return Array.isArray(data)? [...data]:[data]
// ^ Type '(T & any[])[number][]' is not assignable to type 'T extends (infer K)[] ? K[] : T[]'
}
const a = wrapInArray('test') // string[]
const b = wrapInArray([1,2]) // number[]
The function works correctly, and if I try to use the function, typescript correctly infers the return type. Now I'm wondering why I'm getting an error in the function declaration itself.