How to allow only certain string values for variables or constants in TypeScript?
Tags: typescript
Published: 2021-11-01 00:00:00

---
  title: How to allow only certain string values for variables or constants in TypeScript?
  published: true
  tags: TypeScript
  date: Mon Nov 01 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/allow-only-certain-string-values-for-variables-constants-typescript
  cover_image: https://melvingeorge.me/_next/static/images/main-208cf062e792f085678fd4c9e66511ff.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/allow-only-certain-string-values-for-variables-constants-typescript)
  
To allow only certain string values for variables or constants, we can use a `string literal union type` in TypeScript.

### TL;DR

```ts
// string literal union type with
// values of "admin", "owner", "user"
type Roles = "admin" | "owner" | "user";

// use the type "Roles" for the "role" variable
let role: Roles;

// assing a value other than "admin", "owner", "user"
role = "Hello World!"; // ❌ not allowed. Type '"Hello World!"' is not assignable to type 'Roles'

// assign a valid string value
role = "admin"; // ✅ allowed.
```

For example, let's say we have a variable called `role` like this,

```ts
let role = "Hello World!";
```

As you can see from the above code that now we can have any string values assigned to the `role` variable. This is fine for now. But what if we need to restrict the values of the `role` variable to only hold certain string values like `owner`, `admin`, or `user`. To achieve this functionality in TypeScript, we can use `string literal union types` in TypeScript.

To do that first we can define a type called `Roles` and then assign [`string literal union type`](https://melvingeorge.me/blog/set-more-than-one-type-variable-typescript) values of `admin`, `owner`, and `user`.

It can be done like this,

```ts
// string literal union type with
// values of "admin", "owner", "user"
type Roles = "admin" | "owner" | "user";
```

Now let's use the `Roles` type for the `role` variable. It can be done like this,

```ts
// string literal union type with
// values of "admin", "owner", "user"
type Roles = "admin" | "owner" | "user";

// use the type "Roles" for the "role" variable
let role: Roles;
```

Now let's try to assign a value other than the `admin`, `owner` or `user` string values to `role` variable like this,

```ts
// string literal union type with
// values of "admin", "owner", "user"
type Roles = "admin" | "owner" | "user";

// use the type "Roles" for the "role" variable
let role: Roles;

// assing a value other than "admin", "owner", "user"
role = "Hello World!"; // ❌ not allowed. Type '"Hello World!"' is not assignable to type 'Roles'

// assign a valid string value
role = "admin"; // ✅ allowed.
```

As you can see from the above code that the TypeScript compiler won't compile the code if the `role` variable doesn't have a valid string literal value and shows an error saying `Type '"Hello World!"' is not assignable to type 'Roles'` which is exactly what we want to happen.

Also assigning the value of `admin` is allowed since the `admin` string literal value is defined in the `Roles` type.

See the above code live in [codesandbox](https://codesandbox.io/s/allow-only-certain-string-values-for-variables-or-constants-in-typescript-7mu3j?file=/src/index.ts).

That's all 😃!

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

---