Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: support exhaustive match on larger unions #214

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
"typescript": "^5.3.3"
}
}
37 changes: 13 additions & 24 deletions src/types/BuildMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,26 @@ type BuildOne<data, xs extends readonly any[]> = xs extends [
? BuildOne<Update<data, value, Extract<path, readonly PropertyKey[]>>, tail>
: data;

type SafeGet<data, k extends PropertyKey, def> = k extends keyof data
? data[k]
: def;

// Update :: a -> b -> PropertyKey[] -> a
type Update<
data,
value,
path extends readonly PropertyKey[]
> = path extends readonly [infer head, ...infer tail]
type Update<data, value, path> = path extends readonly [
infer head,
...infer tail
]
? data extends readonly [any, ...any]
? head extends number
? UpdateAt<
data,
Iterator<head>,
Update<data[head], value, Extract<tail, readonly PropertyKey[]>>
>
? UpdateAt<data, Iterator<head>, Update<data[head], value, tail>>
: never
: data extends readonly (infer a)[]
? Update<a, value, Extract<tail, readonly PropertyKey[]>>[]
? Update<a, value, tail>[]
: data extends Set<infer a>
? Set<Update<a, value, Extract<tail, readonly PropertyKey[]>>>
? Set<Update<a, value, tail>>
: data extends Map<infer k, infer v>
? Map<k, Update<v, value, Extract<tail, readonly PropertyKey[]>>>
: Compute<
Omit<data, Extract<head, PropertyKey>> & {
[k in Extract<head, PropertyKey>]: Update<
SafeGet<data, k, {}>,
value,
Extract<tail, readonly PropertyKey[]>
>;
? Map<k, Update<v, value, tail>>
: head extends keyof data
? Compute<
{ [k in Exclude<keyof data, head>]: data[k] } & {
[k in head]: Update<data[k], value, tail>;
}
>
: data
: value;
66 changes: 66 additions & 0 deletions tests/large-exhaustive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,70 @@ describe('large exhaustive', () => {
.exhaustive()
).toBe('Null');
});

it('Input with a large distributed cardinality', () => {
type States = 'idle' | 'loading' | 'success' | 'error' | 'partial_result';

const eventStatus = 'success' as States;
const dataStatus = 'loading' as States;
const backgroundStatus = 'loading' as States;
const replaySelectorsStatus = 'idle' as States;

const input = {
eventStatus,
dataStatus,
backgroundStatus,
replaySelectorsStatus,
} as const;

type input = typeof input;

const res = match(input)
.returnType<
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success' }
| { status: 'error'; error: unknown }
>()
.with(
{ eventStatus: P.union('loading', 'partial_result') },
{ dataStatus: P.union('loading', 'partial_result') },
{ backgroundStatus: P.union('loading', 'partial_result') },
{ replaySelectorsStatus: P.union('loading', 'partial_result') },
() => ({ status: 'loading' })
)
.with(
{
eventStatus: 'success',
dataStatus: 'success',
backgroundStatus: 'success',
replaySelectorsStatus: 'success',
},
() => ({ status: 'success' })
)
.with(
{ eventStatus: 'idle' },
{ dataStatus: 'idle' },
{ backgroundStatus: 'idle' },
{ replaySelectorsStatus: 'idle' },
() => ({ status: 'idle' as const })
)
.with({ replaySelectorsStatus: 'error' }, () => ({
status: 'error',
error: new Error('Oops 0'),
}))
.with({ eventStatus: 'error' }, () => ({
status: 'error',
error: new Error('Oops 1'),
}))
.with({ dataStatus: 'error' }, () => ({
status: 'error',
error: new Error('Oops 2'),
}))
.with({ backgroundStatus: 'error' }, () => ({
status: 'error',
error: new Error('Oops 3'),
}))
.exhaustive();
});
});