The Implicit ‘Any’ Type Error
Consider this addTwoNumbers
function:
export const addTwoNumbers = (a, b) => {
return a + b;
};
This function takes in a
and b
and adds them together.
It looks like perfectly valid JavaScript.
Running npm run exercise 01
in the terminal, we can see that our tests pass.
But even though it looks valid and tests pass, TypeScript isn't happy.
The terminal displays the following errors along with the line of code where they happen:
Parameter 'a' implicitly has an 'any' type.
Parameter 'b' implicitly has an 'any' type.
Challenge
Read through the "Migrating from JavaScript" article in the TypeScript docs and see if you can find how to fix these TypeScript errors.
Transcript
We've got some code here that looks like perfectly valid JavaScript, but TypeScript is yelling at us. We've got a function called add two numbers, which takes in a and b and adds them together. We can see in our terminal, in the tests that it's parsing here.
We can mess this up. If we want, we can say a-b and that will start failing our tests. We can see that it is actually working at runtime, but time script is not happy. Your job here is to work out why it's not happy. To decode these error messages, parameter a implicitly has in any type.
What on earth could that mean? Parameter b implicitly has in any type. Have a look in the documentation and see if you can find any hints as to what can fix these TypeScript errors.