You can use TimeScript to throw some absolutely crazy errors at the type level even without touching the runtime code at all.
Here in our deepEqualCompare function we use a === b which is really good for comparing primitives like two numbers or a string to another string but it's really not very good at comparing two arrays.
export const deepEqualCompare = <Arg>(a: Arg, b: Arg): boolean => {
if (Array.isArray(a) && Array.isArray(b)) {
throw new Error("You cannot compare two arrays using deepEqualCompare")
}
}
deepEqualCompare(1, 1)
deepEqualCompare([], ["a"])
The array comparison will always return false, because this first array is not the same element as the other array. We can actually move this to the type level.
first of all we're going to say check for bad args and we're going to say this is going to be. a generic and the arg is going to be like this. We're going to say arg extends any array and we're going to say you cannot compare two arrays using deepEqualCompare. We're just going to copy and. paste that in otherwise we're going to return the arg
type CheckForBadArgs<Arg> = Arg extends any[]
? "You cannot compare two arrays using deepEqualCompare"
: Arg
and we're going to use this in the function arguments of deepEqualCompare. Adding CheckForBadArgs to both a and b.
export const deepEqualCompare = <Arg>(
a: CheckForBadArgs<Arg>,
b: CheckForBadArgs<Arg>
): boolean => {
if (Array.isArray(a) && Array.isArray(b)) {
throw new Error("You cannot compare two arrays using deepEqualCompare")
}
}
And now what you'll see when we deepEqualCompare two arrays is an error saying, "Argument of type 'never[]' is not assignable to parameter of type ''You cannot compare two arrays using deepEqualCompare'".
So we've actually successfully moved this error to the type level!
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.