I was wanting to use conditional types for a rest parameter but I was surprised when I did a test first
Here is a playground and here is the code:
type ExtractParts<P extends any[]> =
P extends [infer H, ...infer U]
? ['yes way']
: ['no way']
interface A {
description: string;
action: () => {
say: string;
it: string;
};
}
type Q = ExtractParts<[1,2,3]> // ['yes]
type P = ExtractParts<A[]>; // ['no way']
type R = ExtractParts<[{ // ['yes way']
description: string;
action: () => {
say: string;
it: string;
};
}]>;
I was surprised by this behaviour
type P = ExtractParts<A[]>; // ['no way']
But this works as I think because it is a strict tuple
type R = ExtractParts<[{ // ['yes way']
description: string;
action: () => {
say: string;
it: string;
};
}]>;
I wanted to use this technique with a rest parameter
type ExtractParts<P extends any[]> =
P extends [infer H, ...infer U]
? ['yes way']
: ['no way']
function parts<P extends any[]>(...parts: ExtractParts<P>){}
const b = parts(1,2,3);
But P extends [infer H, ...infer U] returns false
Can I use conditional types like this to infer the head and tail for rest parameters?