Typescript type boolean is not assignable to void

ber*_*nlt 4 javascript typescript

myContact = [
 {
  name: 'John',
  lastName: 'Doe',
  phone: 123456789
 },
 {
  name: 'Mark',
  lastName: 'Doe',
  phone: 98765432
 }
]
Run Code Online (Sandbox Code Playgroud)

On click event, add a condition to check the array length, if the length > 2.

onClick() {

 if(myContact.length > 2)
     redirect page...
    return false; // don't want the code to continue executing
 }
Run Code Online (Sandbox Code Playgroud)

error: Typescript type boolean is not assignable to void

I try something similar using some(), the below my conditions works as required

let checkValue = myContact.some(s => s.name === 'John')
if(checkValue)return false
Run Code Online (Sandbox Code Playgroud)

But if I try similar with my contact, E.G

let checkLength = myContact.filter(obj => obj.name).length
if(checkValue)return false   // error: error: Typescript type boolean is not assignable to void
Run Code Online (Sandbox Code Playgroud)

How can I fix this issue,

Luz*_*uze 5

A void type means that the function does something but doesn't return a value. That means that a function with the type void can't return a boolean either. As TypeScript expects it to return nothing.

You most likely have a function declared something like this:

const functionName = (): void => {
  ...
}
Run Code Online (Sandbox Code Playgroud)

Besides that, this doesn't seem to be the core of the issue here. If you want your function to "return early" and stop executing the rest of its logic. You can explicitly tell it to return nothing like this:

const functionName = (): void => {
  ...
}
Run Code Online (Sandbox Code Playgroud)