For a string enum, check the enum's values and wrap the check in a type guard: function isColor(value: unknown): value is Color { return (Object.values(Color) as unknown[]).includes(value); }. The cast matters: without it, Object.values(Color).includes(text) fails with error TS2345. For a numeric enum use the reverse mapping instead: typeof value === "number" && Status[value] !== undefined.
TypeScript has no built-in Enum.isDefined, but a regular enum is a real object at runtime, so membership is a lookup in that object. The details depend on the kind of enum: a string enum holds only its values, while a numeric enum also maps every number back to its name, which breaks the naive checks. What TypeScript adds on top of the lookup is the type: written as a type guard, a successful check narrows a plain string or unknown to the enum type, so you can pass it on without a cast. The sections below cover the guard for string enums, the compile error the most common one-liner gives, numeric enums, checking member names instead of values, and one generic guard for every enum. Each example runs on this page: hit Run, then edit the code and run it again.
1A type guard over Object.valuesRecommended
Object.values(Color) gives the values of a string enum, "red", "green" and "blue", and includes tells you whether a value is one of them. Put that in a function whose return type is value is Color, a type predicate, and every if that calls it narrows the value to Color in its true branch. The parameter is unknown so that anything can be checked: a form field, a query parameter or a property of parsed JSON. That is also why the array is widened to unknown[], as the next section explains.
enum Color { Red = "red", Green = "green", Blue = "blue", } // A type guard: when it returns true, TypeScript treats value as a Color. // It takes unknown, so a form field or a JSON property can go straight in. function isColor(value: unknown): value is Color { // Widen the array: Color[] would only accept a Color in includes() return (Object.values(Color) as unknown[]).includes(value); } function paint(color: Color): string { return `painting it ${color}`; } console.log(Object.values(Color)); for (const input of ["green", "purple", "Red", 42]) { if (isColor(input)) { // input is narrowed to Color here, so paint() accepts it console.log(`${input}: ${paint(input)}`); } else { console.log(`${input}: not a Color`); } } // The value is the member itself: no lookup needed after the check const fromForm: string = "blue"; if (isColor(fromForm)) { console.log(fromForm === Color.Blue); }
Output
The values print as [ 'red', 'green', 'blue' ]. Only green: painting it green passes the check: purple and 42 are not values, and Red is the member's name, not its value (for names, see section 4). After the check the value is the member, so fromForm === Color.Blue prints true with no further lookup. To turn a string into an enum member by name, or with a fallback, see convert a string to an enum.
2Why Object.values(Enum).includes(value) fails
The one-liner most answers give is Object.values(Color).includes(value), and it works in JavaScript. In TypeScript it fails as soon as value is a plain string: Object.values(Color) is typed Color[], and includes on a Color[] only accepts a Color. Asking whether a string is a member is exactly what that signature rules out. This embed fails on purpose: the type error stops the run before anything executes.
enum Color { Red = "red", Green = "green", Blue = "blue", } const input: string = "green"; // Object.values(Color) is Color[], so includes() only takes a Color console.log(Object.values(Color).includes(input));
Output
The compiler reports error TS2345: Argument of type 'string' is not assignable to parameter of type 'Color'. Any of these compiles and prints true for "green": widen the array with (Object.values(Color) as string[]).includes(input), pick the element type with Object.values<string>(Color).includes(input), or compare one by one with Object.values(Color).some((v) => v === input). The fourth fix people reach for, includes(input as Color), compiles too, but it asserts the very thing you are checking. Widening the array is safe; narrowing the value is the guard's job.
3Numeric enums: use the reverse mapping
A numeric enum compiles to an object that maps both ways: Status.Active is 1 and Status[1] is "Active". So Object.values returns the names as well as the numbers, and both includes and in accept names and numeric strings as if they were values. The reliable check is the reverse mapping itself: make sure the value is a number, then look it up. A member's number maps back to its name, any other number gives undefined.
enum Status { Draft, // 0 Active, // 1 Closed, // 2 } // A numeric enum object maps both ways: name -> number and number -> name console.log(Object.keys(Status)); console.log(Object.values(Status)); // So the string-enum checks give false positives here console.log(Object.values(Status).includes("Active")); // a name, not a value console.log("1" in Status, "Active" in Status); // Check it is a number, then use the reverse mapping: a member's // number maps back to its name, any other number gives undefined function isStatus(value: unknown): value is Status { return typeof value === "number" && Status[value] !== undefined; } for (const value of [1, 2, 7, 1.5, "Active", "1"]) { console.log(JSON.stringify(value), isStatus(value)); } // Numbers from text: Number("") is 0, which is Status.Draft const empty = ""; console.log(isStatus(Number(empty))); console.log(/^\d+$/.test(empty) && isStatus(Number(empty)));
Output
The keys print as [ '0', '1', '2', 'Draft', 'Active', 'Closed' ] and the values as [ 'Draft', 'Active', 'Closed', 0, 1, 2 ], so includes("Active") is true and so are "1" in Status and "Active" in Status. The guard accepts 1 and 2 and rejects 7, 1.5, the name and the string "1". The last two lines are the trap with text input: Number("") is 0, a valid member, so an empty field passes (true) until you check the text is digits first (false).
4Checking a member name: Object.hasOwn, not in
Sometimes the input is the member's name, such as "Green", not its value. The in operator looks like the answer, and many answers suggest it, but it has two catches. It checks keys, so on a string enum it answers a different question than includes on the values. And it walks the prototype chain, so "toString" in Color is true. Object.hasOwn checks own keys only, and as a guard returning key is keyof typeof Color it also lets you index the enum with the key.
enum Color { Red = "red", Green = "green", Blue = "blue", } // in checks member NAMES (the keys), not the values... console.log("Red" in Color, "red" in Color); // ...and it also finds keys inherited from Object.prototype console.log("toString" in Color); // Own keys only, as a guard so that Color[key] type-checks function isColorKey(key: string): key is keyof typeof Color { return Object.hasOwn(Color, key); } for (const key of ["Green", "green", "toString"]) { if (isColorKey(key)) { console.log(`${key} -> ${Color[key]}`); } else { console.log(`${key}: not a Color name`); } } // Numeric enums also own their reverse keys "0", "1", ... enum Status { Draft, Active, } console.log(Object.hasOwn(Status, "0"), Number.isNaN(Number("0")));
Output
"Red" in Color is true but "red" in Color is false, and "toString" in Color is true. The guard prints Green -> green and rejects the other two. An in check also doesn't narrow the key: after if (key in Color), Color[key] with key: string runs on this page, whose compiler has strict off, but with strict on, as in most projects, it fails with error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Color'. On a numeric enum, filter out the reverse keys too: Object.hasOwn(Status, "0") is true, and Number.isNaN(Number(key)) is false for them.
5One guard for every enum
When a codebase has many enums, write the check once. enumValues drops the reverse keys (the ones that parse as numbers), so it returns the real values of string and numeric enums alike. enumGuard puts them in a Set and returns a type guard whose predicate is value is E[keyof E], which for typeof Color is just Color. Building the guard once per enum also means each check is a Set lookup instead of a fresh Object.values array, which matters when you validate thousands of rows.
type EnumObject = Record<string, string | number>; // Every value of an enum, minus the number-to-name entries that // numeric enums add (their keys are "0", "1", ...) function enumValues<E extends EnumObject>(e: E): E[keyof E][] { return Object.keys(e) .filter((key) => Number.isNaN(Number(key))) .map((key) => e[key as keyof E]); } // Build the guard once per enum: a Set lookup per check function enumGuard<E extends EnumObject>(e: E): (value: unknown) => value is E[keyof E] { const values = new Set<unknown>(enumValues(e)); return (value: unknown): value is E[keyof E] => values.has(value); } enum Color { Red = "red", Green = "green", Blue = "blue", } enum Status { Draft, Active, Closed, } const isColor = enumGuard(Color); const isStatus = enumGuard(Status); console.log(enumValues(Color), enumValues(Status)); function describe(color: Color, status: Status): string { return `${color} item, ${Status[status]}`; } // Rows as they might arrive from JSON: anything can be in them const rows: unknown[][] = [["red", 1], ["Red", 1], ["blue", "Active"], ["green", 2]]; for (const [color, status] of rows) { if (isColor(color) && isStatus(status)) { console.log(describe(color, status)); } else { console.log(`rejected ${JSON.stringify([color, status])}`); } }
Output
The values print as [ 'red', 'green', 'blue' ] [ 0, 1, 2 ], without the numeric enum's names. Two rows pass and reach describe, which takes a Color and a Status and gets them without a cast: red item, Active and green item, Closed. The other two are rejected, one for a member name ("Red"), one for a name where a number belongs ("Active"). The same code type-checks with strict on. For narrowing other shapes with your own guards, see check a type at runtime.
6Which should you use?
| Method | String enum | Numeric enum | Narrows the type |
|---|---|---|---|
| (Object.values(E) as unknown[]).includes(v) in a guard | Correct | Also matches names | Yes, value is E |
| Object.values(E).includes(str) | TS2345 for a string | Compiles, matches names | No |
| typeof v === "number" && E[v] !== undefined | Not applicable | Correct | In a guard |
| key in E | Checks names, finds toString | Names and numbers | No |
| Object.hasOwn(E, key) | Names only | Names plus "0", "1"… | In a guard (keyof typeof E) |
| enumGuard(E) with a Set | Correct | Correct | Yes, value is E |
Frequently asked questions
Does TypeScript have something like C#'s Enum.IsDefined?
No. An enum compiles to a plain object and TypeScript adds no methods to it, so you write the check yourself: (Object.values(Color) as unknown[]).includes(value) for a string enum, typeof value === "number" && Status[value] !== undefined for a numeric one, or a generic enumGuard(E) that handles both. Declaring it as a type guard (value is Color) is what makes it more useful than includes alone.
How do I check if a value exists in a const enum?
You cannot at runtime: a const enum is inlined at compile time and leaves no object behind, so Object.values(Flag) fails with error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query. Use a regular enum, or an as const object whose values you can list.
Why does "toString" in MyEnum return true?
The in operator checks the whole prototype chain, and every enum object inherits toString, hasOwnProperty and the rest from Object.prototype. Use Object.hasOwn(MyEnum, key) to check own keys only. Remember too that in checks member names: on a string enum "Red" in Color is true while the value "red" gives false.
Why does Object.values return the names of a numeric enum?
Numeric enums get a reverse mapping: enum Status { Draft, Active, Closed } compiles to an object with both Draft: 0 and 0: "Draft". Object.values(Status) therefore prints [ 'Draft', 'Active', 'Closed', 0, 1, 2 ]. Check typeof value === "number" first, or filter out the keys that parse as numbers. String enums have no reverse mapping, so their values are just the strings.
How do I check a value against a union type instead of an enum?
A union type such as "red" | "green" has no runtime object, so keep its values in an array declared as const and derive the type from it: const COLORS = ["red", "green"] as const; type Color = (typeof COLORS)[number];. The guard is then (COLORS as readonly string[]).includes(value). The create a union type from an array example covers that pattern.
After the check, how do I get the enum member?
For a value check you already have it: a string enum member is its value, so once isColor(text) is true, text is typed Color and text === Color.Blue is true. For a name check, index the enum with the narrowed key: Color[key] gives "green" for "Green". Converting with a fallback for unknown strings is covered in the convert a string to an enum example.
Run it yourself
Open any of these in the full TypeScript editor to tweak, run and share.
TypeScript playground