How to Require a Specific String in TypeScript Interface ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report To require a specific string in the TypeScript interface, we have different approaches. In this article, we are going to learn how to require a specific string in the TypeScript interface. Below are the approaches used to require a specific string in the TypeScript interface: Table of Content Using String Literal TypeUsing EnumApproach 1: Using String Literal TypeThis approach utilizes string literal types in TypeScript interfaces to define a property with specific allowed string values. In the below example, Interface Definition (TaskStatus):The TaskStatus interface is defined with a property named status.The status property is restricted to having one of three specific string values: "todo", "in-progress", or "done" using string literal types.Valid Object (task1):An object task1 is created with the status property set to a valid value, "todo".The TypeScript compiler allows this object because "todo" is one of the specified string values in the interface.Example: Here, the TaskStatus interface restricts the status property to the string values "todo", "in-progress", or "done". JavaScript // Using String Literal Type interface TaskStatus { status: "todo" | "in-progress" | "done"; } // Valid object const task1: TaskStatus = { status: "todo" }; console.log(task1.status); // Output: "todo" Output: todoApproach 2: Using EnumEnums provide a way to define a set of named constant values. In this approach, an enum is used to create a set of allowed string values for a property in an interface. In the below example, Enum Definition (Weekday):An enum named Weekday is defined, where each enum member is assigned a specific string value representing days of the week.Interface Definition (Schedule):The Schedule interface is defined with a property named day.The day property is restricted to having one of the enum values from Weekday.Valid Object (meeting):An object meeting is created with the day property set to a valid enum value, Weekday.Wednesday.The TypeScript compiler allows this object because Wednesday is a valid enum value.Example: Here, the Weekday enum is used to restrict the day property in the Schedule interface to specific string values. JavaScript // Using Enum enum Weekday { Monday = "Monday", Tuesday = "Tuesday", Wednesday = "Wednesday", Thursday = "Thursday", Friday = "Friday", } interface Schedule { day: Weekday; } // Valid object const meeting: Schedule = { day: Weekday.Wednesday }; console.log(meeting.day); // Output: "Wednesday" Output: Wednesday Comment More infoAdvertise with us Next Article How to check interface type in TypeScript ? A amanv09 Follow Improve Article Tags : TypeScript Similar Reads How to Cast Object to Interface in TypeScript ? In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU 3 min read How to Extract Interface Members in TypeScript ? In TypeScript, you can extract interface members (properties and methods) from a class using several approaches. we are going to learn how to extract interface members in TypeScript. Below are the approaches used to extract interface members in TypeScript: Table of Content Manual Extractionimplement 3 min read How to parse JSON string in Typescript? In this tutorial, we will learn how we can parse a JSON string in TypeScript. The main reason for learning about it is to learn how we can explicitly type the resulting string to a matching type. The JSON.parse() method will be used to parse the JSON string by passing the parsing string as a paramet 3 min read How to Define Interfaces for Nested Objects in TypeScript ? In TypeScript, defining interfaces for nested objects involves specifying the structure of each level within the object hierarchy. This helps ensure that the nested objects adhere to a specific shape or pattern. Here are step-by-step instructions on how to define interfaces for nested objects in Typ 2 min read How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in 2 min read How to Make a Single Property Optional in TypeScript ? TypeScript is a popular programming language that is a strict syntactical superset of JavaScript, adding optional static typing and class-based object-oriented programming to the language. One useful feature of TypeScript is the ability to specify optional properties in interfaces and classes, allow 5 min read Like