Skip to main content

Enums

This page pertains to user-defined enums, not roblox enums.

API​

Type of enum​

You can use the enum method to check if a value is a valid enum.

import { expect } from "@rbxts/expect";

enum Sport {
Basketball,
Football,
Soccer,
}

expect(Sport.Basketball).to.be.the.enum(Sport);
expect("Basketball").to.be.the.enum(Sport);
expect(0).to.be.the.enum(Sport);

Example Error​

Expected '5' (number) to be a valid enum of '(First | Second | Third)'

Enum value​

You can use an overload of the enum method to check if a value is a specific enum value.

import { expect } from "@rbxts/expect";

enum Sport {
Basketball,
Football,
Soccer,
}

expect(Sport.Basketball).to.be.the.enum(Sport, "Soccer");

Example Error​

Expected 'Basketball' (enum/number) to be the enum 'Soccer'

Chaining​

AnyOf​

Calling anyOf after an enum call will cause failure messages to use the enum's table keys instead of their actual value, with the type being represented as either (enum/number) or (enum/string).

import { expect } from "@rbxts/expect";

enum Sport {
Basketball,
Football,
Soccer,
}

expect("Basketball").to.be.the.enum(Sport).and.to.be.anyOf([2, 3]);

Example Error​

Expected 'Basketball' (enum/number) to be any of '["Football", "Soccer"]'