I’m working on some type guards for an API framework and want to connect the path parameters (a string) to a validation object.
What I want:
My validation object looks like this:
const params = {
firstArg: someValidationFunction,
secondArg: someValidationFunction,
}
Given the following strings, I want the following results:
/api/some/path/{firstArg}/{secondArg}
-> OK (both arguments appear in the string by the right name)/api/some/path/{secondArg}/{firstArg}
-> OK (the order of arguments cannot be enforced)/api/some/path/{someOtherArg}
-> Not OK (both args are missing and there is an unexpected arg found)/api/some/path/{firstArg}/{secondArg}/{someOtherArg}
-> Not OK (there is an unexpected arg found)/api/some/path/{firstArg}/secondArg
-> Not OK (second arg is missing since it doesn’t have the curly braces)
What I have so far:
Following this blog I have this type helper:
type ExtractPathParams<Path> = Path extends `${infer Segment}/${infer Rest}`
? ExtractParam<Segment, ExtractPathParams<Rest>>
: ExtractParam<Path, {}>;
type ExtractParam<Path, NextPart> = Path extends `{${infer Param}}`
? Record<Param, any> & NextPart
: NextPart;
Which gives me the arguments found in the path, and I’ve been trying to take the keys from that and the keys from the validation object and expect them to be the same by doing:
type PathKeys = keyof ExtractPathParams<typeof path>;
type ValidationKeys = keyof TypeOf<typeof schemaForParam>;
type IsEqual<T extends U, U> = T extends U ? (U extends T ? 'ok' : 'not_ok') : 'not_ok';
type TestIsEqual = IsEqual<PathKeys, ValidationKeys>;
type ErrorIfNotEqual<T extends 'ok' = TestIsEqual> = void;
But this doesn’t seem to work, TestIsEqual
becomes the type 'ok' | 'not_ok'
which ends up not working when used with ErrorIfNotEqual
(it needs to become either ok
or not_ok
, not the union of both).
I think another challenge is that if no arguments are put in the string, the extraction helper returns an empty object which when passed to keyof
becomes the never
type.
I would also really prefer if it could produce a type error stating which keys are missing in PathKeys
based on which keys appear in ValidationKeys
(rather than saying that not_ok
isn’t assignable to ok
).
The current code I have solves the issues for when an unexpected argument is found, but not when an expected argument is missing. I tried adding U extends T
in IsEqual
but that (obviously) results in a circular constraint.
Perhaps there is another approach to compare if two object shapes are the same or to compare if two string unions are the same.