I have this code
type Check<F, T> = F extends string
? string
: T
type Func2 = <F extends T[] | string, T>(functor: F) => Check<F, T>
const x: Func2 = (x: any) => x[0]
const y = x([1, 2])
const z = x("abc")
I am getting type unknown for y while it is perfectly fine for z.
The type inferred for calling x for y is -
const x: <number[], unknown>(functor: number[]) => unknown
Why there is unknown for T but T[] is inferred properly?
Tin the signature ofx.Fcan be inferred by the type offunctor, but nothing you're passing in will fixTso it falls back tounknown. And soFis only constrained tounknown[] | string. I'm not sure why you'd prefer your definition ofFunc2over something liketype Func2 = <F extends { [k: number]: any }>(functor: F) => F[0]. Can you explain what you're trying to do and why you're trying to do it this way?