Allow only specific string values with TypeScript type

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Allow only specific string values with TypeScript type

Use a string literal type to only allow specific string values in a TypeScript type, e.g. const str: 'draft' | 'sent' = 'draft';.

String literals allow us to refer to specific strings in type positions. If the specified string is not in the literal type, an error is thrown.

index.ts
// ✅ with an interface interface Person { name: 'Alice' | 'Bob' | 'Carl'; } const p: Person = { name: 'Alice', }; // ✅ with a Type Alias type Sizes = 'small' | 'medium' | 'large'; const s: Sizes = 'small';

allow only specific string values with type

The code for this article is available on GitHub

Here are two more examples.

index.ts
// ✅ inline const str: 'draft' | 'sent' = 'draft'; // ✅ for function parameters function logMessage(message: 'hello world' | 'howdy world') { console.log(message); } logMessage('hello world');

two more examples

The examples show how to use a string literal type to only allow assignment to specific string values.

index.ts
interface Person { name: 'Alice' | 'Bobby Hadz' | 'Carl'; } const p: Person = { name: 'Bobby Hadz', }; console.log(p.name); // 👉️ Bobby Hadz

using string literal type

String literal types allow us to refer to specific strings in type positions.

Assignment of a string that is not a member of the literal type causes an error.

index.ts
// ⛔️ Type '"bobbyhadz.com"' is not assignable // to type '"draft" | "sent"'. const str: 'draft' | 'sent' = 'bobbyhadz.com';

An easy way to think about string literal types is that they are a union type with specific string values instead of types.

You can also use a string literal type when typing a function's parameter.

index.ts
function logMessage(message: 'hello world' | 'howdy world') { console.log(message); } // ✅ Works logMessage('hello world'); // ⛔️ Error logMessage('bobbyhadz.com');

The function can be called with one of two strings and trying to call it with any other value causes an error.

Literal types can also be used for numbers and booleans. For example, the type boolean is just an alias for the union true | false.

# Allow only specific string values with TypeScript type using an enum

Alternatively, you could use an enum.

Enums allow us to define a set of named constants. If the specified string is not a member of the enum, an error is thrown.

index.ts
enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // ✅ using interfaces interface JobEmails { status: EmailStatus; } const obj: JobEmails = { status: EmailStatus.Read, }; // ✅ inline assignment const emailStatus: EmailStatus = EmailStatus.Read;

allow only specific string values using enum

The code for this article is available on GitHub

Enums are sometimes easier to read than string literal types.

Note that enums are real objects and exist at runtime. You can use dot notation to access a property on an enum.

The emailStatus variable can only have one of the 3 values that are present in the EmailStatus enum.

An assignment of a different string would cause the type checker to throw an error.

index.ts
enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', } // ⛔️ Error: Type '"bobbyhadz.com"' is not // assignable to type 'EmailStatus'.ts(2322) const emailStatus: EmailStatus = 'bobbyhadz.com';

The main advantage of using enums over string literal types is that they make your code easier to read and organize.

The whole purpose of enums is to define a group of named and related constants.

I've also written an article on how to check if a string is in a union.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.