How to add a boolean type annotation to a function parameter in TypeScript?
Tags: typescript
Published: 2021-09-10 00:00:00

---
  title: How to add a boolean type annotation to a function parameter in TypeScript?
  published: true
  tags: TypeScript
  date: Fri Sep 10 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/add-boolean-type-annotation-to-function-parameters-typescript
  cover_image: https://melvingeorge.me/_next/static/images/main-06cc2acb4b84c1b5593919996877d66b.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/add-boolean-type-annotation-to-function-parameters-typescript)
  
Boolean type annotations can be added to function parameters by declaring the function and then adding the name of the parameter followed by the `:` symbol (colon) and then the type `boolean` after the symbol.

### TL;DR

```ts
// add boolean type annotation
// to the parameter isAdmin
function setAdmin(isAdmin: boolean) {
  if (isAdmin) {
    console.log("Admin set");
  } else {
    console.log("Admin not set");
  }
}

// call the function
setAdmin(true); // ✅ allowed
```

For example, let's say we have a function called `setAdmin` and on calling the function checks to see if the boolean type parameter `isAdmin` is `true` or `false` and log output accordingly.

So to get the boolean value we can use a parameter called `isAdmin`. It can be done like this,

```ts
// a simple function
function setAdmin(isAdmin) {
  if (isAdmin) {
    console.log("Admin set");
  } else {
    console.log("Admin not set");
  }
}
```

Now we need to only allow `boolean` type values as the value for the parameter `isAdmin`. To do that we can add a `boolean` type annotation to the function parameter. In our case, the parameter is `isAdmin`.

It can be done like this,

```ts
// add boolean type annotation to
// the parameter isAdmin
function setAdmin(isAdmin: boolean) {
  if (isAdmin) {
    console.log("Admin set");
  } else {
    console.log("Admin not set");
  }
}
```

Now let's try to call the function `setAdmin` and then pass a boolean value as an argument to the function like this,

```ts
// add boolean type annotation
// to the parameter isAdmin
function setAdmin(isAdmin: boolean) {
  if (isAdmin) {
    console.log("Admin set");
  } else {
    console.log("Admin not set");
  }
}

// call the function
setAdmin(true); // ✅ allowed
```

As you can see from the above code TypeScript allowed us to pass the boolean value of `true` to the function parameter `isAdmin` since the allowed type for the `isAdmin` function parameter is a `boolean`.

Now let's try to pass another value of `string` type `Hello!` to the `isAdmin` function parameter to see what happens,

```ts
// add boolean type annotation
// to the parameter isAdmin
function setAdmin(isAdmin: boolean) {
  if (isAdmin) {
    console.log("Admin set");
  } else {
    console.log("Admin not set");
  }
}

// call the function
setAdmin(true); // ✅ allowed
setAdmin("Hello"); // ❌ not allowed. Argument of type 'string' is not assignable to parameter of type 'boolean'.
```

As you can see that the TypeScript will throw an error saying that the `Argument of type 'string' is not assignable to parameter of type 'boolean'.` which is also what we want to happen.

See the above codes live in [codesandbox](https://codesandbox.io/s/add-boolean-type-annotation-to-function-parameters-in-typescript-xig0r?file=/src/index.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---