Essential Types And Notations 20 exercises

Understanding Function Errors

We start with this add function which takes two boolean parameters a and b and returns a + b:

export const add = (a: boolean, b: boolean) => {
return a + b; // red squiggly line under a + b

A result variable is created by calling the add function. The result variable i

Loading exercise


00:00 In this exercise, we have a function called add, where we have a parameter of A, which is a Boolean, and B, which is also a Boolean, and it returns A plus B. You can see here that there might be something throwing you off, and this code might look a bit weird, and TypeScript agrees.

00:15 It's giving us an error here saying operator plus cannot be applied to types Boolean and Boolean. And down here, we have a function which is calling add, and we have 1 and 2 being passed in, and number is not assignable to parameter of type Boolean.

00:30 Finally, we're checking result down here, and we're checking whether result is equal to number, and it's not. It's currently equal to any. So this is a bit of a common sense exercise. You don't need to know much about TypeScript to solve this. And let me see if by changing the code up here in the code section, you can fix it. Good luck.