Enums
This page documents the enums introduced in Style-Dictionary. Enums provide a set of named constants that enhance code maintainability, readability, and type safety.
Although Style-Dictionary offers TypeScript type definition files, it cannot provide actual TypeScript enums because its code base is written in JavaScript using JSDocs type annotations, and real enums are a TypeScript-only feature. To still leverage the benefits of enums and reduce the use of hardcoded strings throughout the JavaScript codebase of Style-Dictionary itself, we have introduced enum-like JavaScript objects, which provide the same kind of type safety, but can also be used in JavaScript projects.
These enum-like objects are used internally within Style-Dictionary, and you can also use them in your own configurations, whether you are working with TypeScript or JavaScript.
Enums Usage Example
The following shows how to use some of the provided enum-like objects in an exmaple Style-Dictionary configuration.
Read-Only Enums in Typescript
Optionally, if you want to ensure that the enums are completely read-only, you can use as const
, like it is described in the Typescript docs.
This means a type error will also be shown if the enum itself is being assigned to or if something attempts to introduce or delete a member.
List of Enums
Actions
Comment Positions
Comment Styles
File Header Comment Styles
Formats
Log Broken Reference Levels
Log Verbosity Levels
Log Warning Levels
Property Format Names
Transform Groups
Transforms
Transform Types
Benefits of Using Enums
Enums, or enumerations, offer a robust way to define a set of named constants in your code. Unlike hardcoded string values, enums provide several key benefits:
- Consistency: Enums centralize the definition of constants, making it easier to manage and update them across your codebase. This reduces the risk of typos and inconsistencies that can occur with hardcoded strings. This improves maintainability.
- Readability: By using descriptive names for constants, enums make your code more readable and self-documenting. This helps other developers understand the purpose and usage of the constants without needing to refer to external documentation.
- Type Safety: Enums can provide better type checking during development, catching errors at compile time rather than runtime. This ensures that only valid values are used, reducing the likelihood of bugs.
- Future-proofing: Enums offer greater flexibility for future changes. When you need to add or modify values, you can do so in a single location without having to search and replace hardcoded strings throughout your code. This also means that on the consumer side, such a change is not a breaking change.