// docs / enums

Enums

TypeSharp supports two enum output modes controlled directly from your C# code — standard export enum via [TypeSharp], or a const object with a derived union type via [TypeSharp][Union].

Standard enum

default

Decorate any C# enum with [TypeSharp] to generate a standard TypeScript enum with string values.

UserRole.cs — typesharp
UserRole.cs
userRole.ts
[TypeSharp]
public enum UserRole
{
    Guest,
    Member,
    Admin
}
/**
 * Auto-generated by TypeSharp
 * Do not edit this file manually
 */

export enum UserRole {
    Guest = 'Guest',
    Member = 'Member',
    Admin = 'Admin',
}
⎇ main✓ Prettier
csharpUTF-8Ln 1, Col 1

Union enum — [Union]

Add [Union] alongside [TypeSharp] to generate a const object with a derived union type instead. This gives better type safety, tree-shaking, and works naturally with discriminated union patterns.

UserRole.cs — typesharp
UserRole.cs
userRole.ts
[TypeSharp]
[Union]
public enum UserRole
{
    Guest,
    Member,
    Admin
}
/**
 * Auto-generated by TypeSharp
 * Do not edit this file manually
 */

export const UserRole = {
    Guest: 'Guest',
    Member: 'Member',
    Admin: 'Admin',
} as const;

export type UserRole = typeof UserRole[keyof typeof UserRole];
⎇ main✓ Prettier
csharpUTF-8Ln 1, Col 1

Attribute combinations

[TypeSharp] and [Union] can be combined in any order or syntax.

combinations.cs — typesharp
combinations.cs
// Stacked
[TypeSharp]
[Union]
public enum Status { ... }

// Reverse order
[Union]
[TypeSharp]
public enum Status { ... }

// Single bracket
[TypeSharp, Union]
public enum Status { ... }

// With name override
[TypeSharp("status_type"), Union]
public enum Status { ... }
⎇ main✓ Prettier
csharpUTF-8Ln 1, Col 1

Usage in interfaces

Enum references in other decorated classes are resolved automatically.

userDto.ts — typesharp
userDto.ts
export interface UserDto {
    id: number;
    name: string;
    role: UserRole;  // resolved enum reference
}
⎇ main✓ Prettier
typescriptUTF-8Ln 1, Col 1

Usage in your frontend

example.vue — typesharp
example.vue
example.tsx
import { UserRole } from '~/app/types/userRole.ts'
import type { UserDto } from '~/app/types/userDto.ts'

// Access values via the const object
const user = ref<UserDto | null>(null);
const isAdmin = computed(() => user.value?.role === UserRole.Admin);
import { UserRole } from './app/types/userRole.ts'
import type { UserDto } from './app/types/userDto.ts'

// Access values via the const object
const [user, setUser] = useState<UserDto | null>(null);
const isAdmin = user?.role === UserRole.Admin;
⎇ main✓ Prettier
typescriptUTF-8Ln 1, Col 1

Comparison

Feature[TypeSharp][TypeSharp][Union]
Outputexport enumexport const … as const
Typeenum typeliteral union type
Value accessUserRole.AdminUserRole.Admin
Discriminated unionsLess ideal✅ Ideal
Tree-shakeableDepends on bundler✅ Yes