1

A function can cause a type error even if it's never called. What does it means?

I was going through a course on typeScript, now I am stuck on a simple problem that I don't understand.

function add1(n: number): number {
  return n + true
}

3 Answers 3

3

The typescript compiler 'compiles' your code (Turn it into Javascript) before it is run. During the compilation, it checks if there is any error in your code.

In your code n is a number and true is a boolean, you cannot add boolean and a number together. As a result the compiler tells you that it is wrong.


Edit to answer OP's question in the comment:

The course is showing you an example where the code will not work. It is not working because it is not meant to work. Each variable in the code has a type. For example, 'n' has the type of 'number' and 'true' has the type 'boolean'. Those types are not compatible in the operation '+' and that's what is called a type error :) Do you have to put code into the terminal? because the code is not meant to work unless you do what Kevin suggested or replace 'true' with a number.

Sign up to request clarification or add additional context in comments.

3 Comments

To add to this, if you really want to cast the boolean and add it to the number, run it through the Number() function first which will cast it to a number. function add1(n: number): number { return n + Number(true) }
that i understand. There is this course on executeprogram.com in which I'm stuck on a problem. There is terminal in that where i need to run the code. I tried add1(2) but it doesn't pass. I need to somehow execute it in the compiler.. There's an easy way to see this: a function can cause a type error even if it's never called. > function add1(n: number): number { return n + true }
a function can cause a type error even if it's never called. Any idea what does it mean and what should I write in terminal provided
1

Its simple you cant use the operator "+" between a number and a boolean value "return n + true " its wrong you can use it for similar types and others cases but not that one.

edit: yes a function can cause a type error even if its not called its one of the "good things" of the strong type languages they can cause errors before compilation so its easier to debug because they wont let you have type errors

2 Comments

that i understand. There is this course on executeprogram.com in which I'm stuck on a problem. There is terminal in that where i need to run the code. I tried add1(2) but it doesn't pass. I need to somehow execute it in the compiler.. There's an easy way to see this: a function can cause a type error even if it's never called. > function add1(n: number): number { return n + true }
a function can cause a type error even if it's never called. Any idea what does it mean and what should I write in terminal provided
0

Actually, I am afraid you are not clear with the type declaration.

Let me elaborate it for you, hope that might help you.

So, your function is this.

function add1(n: number): number {
  return n + true
}

Now here the parameter which you have used is n and type of n is number. So you are expecting anyone who want to use this function to pass a number which you will add with a 'true'.

Which does not make sense as why will you add number to a boolean.

Now second type which is declared by you. i.e. add1(n: number): number.

This implies that whatever the function will return will be of number type.

See you return statement, it is saying that it will add a number with boolean. So while transpiling your code, typescript is showing A function can cause a type error even if it's never called

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.