Variables
Apply the TypeScript Style Guide's variable conventions in the context of the current task.
Workflow
- Inspect the consuming repository's conventions and configuration.
- Let explicit repository conventions take precedence over this opinionated guidance.
- Apply, review, or explain only the guidance relevant to the task.
- State important tradeoffs when the appropriate choice depends on context or judgment.
Boundaries
- Keep TypeScript and ESLint responsible for checks they can enforce automatically.
- Do not introduce unrelated TypeScript Style Guide conventions merely because this skill is active.
Related Guidance
Application State
For detailed guidance on states that require different data, use typescript-discriminated-unions when it is available.
Variables
Const Assertion
Strive to declare constants using the const assertion as const:
Constants are used to represent values that are not meant to change, ensuring reliability and consistency in a codebase. Const assertions preserve literal types and infer readonly properties.
- Type Narrowing - Using
as constensures that literal values (e.g., numbers, strings) are treated as exact values instead of generalized types likenumberorstring. - Readonly Properties - Objects and arrays get readonly properties, so TypeScript catches direct mutations.
Examples:
-
Objects
-
Arrays
-
Template Literals
Enums & Const Assertion
Enums are discouraged in the TypeScript ecosystem due to their runtime cost and quirks.
The TypeScript documentation outlines several pitfalls, and TypeScript 5.8 introduced the --erasableSyntaxOnly flag to disable runtime-generating features like enums altogether.
<Rule href="https://eslint.org/docs/latest/rules/no-restricted-syntax">{'no-restricted-syntax': [ 'error', { selector: 'TSEnumDeclaration', message: 'Replace enum with a literal type or a const assertion.', }, ]}</Rule>
As a rule of thumb, prefer:
- Literal types whenever possible.
- Const assertion arrays when looping through values.
- Const assertion objects when enumerating arbitrary values.
Examples:
-
Use literal types to avoid runtime objects and reduce bundle size.
-
Use const assertion arrays when looping through values.
-
Use const assertion objects when enumerating arbitrary values.
Type Union & Boolean Flags
Embrace type unions, especially when type union options are mutually exclusive, instead multiple boolean flag variables.
Boolean flags have a tendency to accumulate over time, leading to confusing and error-prone code, since they hide the actual app state.
Use a literal union when only the state value changes. When each state requires different data, use a discriminated union to represent the valid states explicitly.
Null & Undefined
With strictNullChecks, null and undefined have distinct types and meanings. Use them consistently based on what absence means in the application.
Strive to:
- Use
nullwhen a value is explicitly empty, such as an assignment or function return value. - Use
undefinedwhen a value is missing or omitted, such as an optional field in a form, request payload, or database query (Prisma differentiation).


