0

I am trying to figure out a typescript situation where I am trying to define the output of a arrow function.

let isThisTrue:boolean = () => {
  return true;
};

console.log(isThisTrue());

Can I only define a type when I am passing a value to a method? I know this example isn't really practical but I am just trying to grasp why I am unable to say that the expected output is a boolean.

Fiddle: https://jsfiddle.net/mn9efLtg/

Error: Uncaught SyntaxError: Unexpected token :

1 Answer 1

3

The type of isThisTrue is function. The result value of the function is Boolean (typescript playground):

const isThisTrue = ():boolean => {
  return true;
};

console.log(isThisTrue());
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, that makes more sense. I assume const is also used because a function should always be the same in this case.
@SBB - That's entirely up to you. If you intend to change isThisTrue at some point later in the code, use let. If you don't, then you can use const to A) document that fact for future maintainers of the code, and B) have the JavaScript engine enforce it (by raising an error) if you accidentally do try to change its value. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.