You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Source provides no match for required element at position 0 in target despite source extending [any, ...any[]]Β #48401
typeRecursiveType<TListextends[any, ...any[]]>=((
...list: TList)=>unknown)extends(h: infer Head, ...tail: infer Tail)=>unknown
? ((h: Head)=>unknown)extends(h: Array<infer R>)=>unknown
? R
: ((h: Head, ...tail: Tail)=>unknown)extends(h: any,
...tail: [any, ...any[]]// ensure that Tail extends [any, ...any[]])=>unknown
? RecursiveType<Tail>// Error: Source provides no match for required element at position 0 in target
: unknown
: unknown;
π Actual behavior
Comile error:
Error: Source provides no match for required element at position 0 in target
π Expected behavior
We should be able to pass Tail back into RecursiveType as we are ensuring that Tail extends [any, ...any[]]. I have tried this a few different ways, by re-inferring the type of the tail and by switching the Tail and [any, ...any[]] around so we check that [any, ...any[]] extends Tail instead.
We don't have a way to indirectly propagate the facts of the parent constraint into the infer result in a way that is still generic but allows it to directly pass constraint checks. #48112 should help with this use case
Activity
MartinJohns commentedon Mar 24, 2022
Simple workaround:
...tail: [infer TH, ...infer TT]andRecursiveType<[TH, ...TT]>Roaders commentedon Mar 24, 2022
yes, it does. Thanks very much for the workaround. It doesn't work when I specify the type of the items in the tuple though:
Playground
RyanCavanaugh commentedon Mar 28, 2022
We don't have a way to indirectly propagate the facts of the parent constraint into the
inferresult in a way that is still generic but allows it to directly pass constraint checks. #48112 should help with this use caseRoaders commentedon Mar 29, 2022
Yes, that would have definitely helped in several places. I look forward to it being merged. Thanks
50an6xy06r6n commentedon Aug 4, 2023
Is this why
T[]is not assignable to[T, ...T[]] | []?That's a degenerate case, but I have some use for things like this:
const f = <T,>(p: readonly [G<T>, ...G[]] | []) => {}