fix(puppeteer-core): avoid type instantiation errors (#9370)

Using the accumulator allows enabling the tail-recursion optimization in
the TypeScript compiler.

Closes #9369
This commit is contained in:
Alex Rudenko 2022-12-06 19:21:08 +01:00 committed by GitHub
parent be7626bad8
commit 17f31a9ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 17 deletions

View File

@ -135,32 +135,36 @@ type BeginSubclassSelectorTokens = ['.', '#', '[', ':'];
type CombinatorTokens = [' ', '>', '+', '~', '|', '|'];
type Drop<Arr extends readonly unknown[], Remove> = Arr extends [
infer Head,
...infer Tail
]
type Drop<
Arr extends readonly unknown[],
Remove,
Acc extends unknown[] = []
> = Arr extends [infer Head, ...infer Tail]
? Head extends Remove
? Drop<Tail, Remove>
: [Head, ...Drop<Tail, Remove>]
: [];
: Drop<Tail, Remove, [...Acc, Head]>
: Acc;
type FlatmapSplitWithDelemiters<
Inputs extends readonly string[],
Delemiters extends readonly string[]
Delemiters extends readonly string[],
Acc extends string[] = []
> = Inputs extends [infer FirstInput, ...infer RestInputs]
? FirstInput extends string
? RestInputs extends readonly string[]
? [
...SplitWithDelemiters<FirstInput, Delemiters>,
...FlatmapSplitWithDelemiters<RestInputs, Delemiters>
]
: never
: never
: [];
? FlatmapSplitWithDelemiters<
RestInputs,
Delemiters,
[...Acc, ...SplitWithDelemiters<FirstInput, Delemiters>]
>
: Acc
: Acc
: Acc;
type Split<
Input extends string,
Delemiter extends string
Delemiter extends string,
Acc extends string[] = []
> = Input extends `${infer Prefix}${Delemiter}${infer Suffix}`
? [Prefix, ...Split<Suffix, Delemiter>]
: [Input];
? Split<Suffix, Delemiter, [...Acc, Prefix]>
: [...Acc, Input];

View File

@ -6,6 +6,18 @@ declare const nodeFor: <Selector extends string>(
) => NodeFor<Selector>;
{
{
expectType<HTMLTableRowElement>(
nodeFor(
'[data-testid="my-component"] div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div tbody tr'
)
);
expectNotType<Element>(
nodeFor(
'[data-testid="my-component"] div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div div tbody tr'
)
);
}
{
expectType<HTMLAnchorElement>(nodeFor('a'));
expectNotType<Element>(nodeFor('a'));