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].
Decorate any C# enum with [TypeSharp] to generate a standard TypeScript enum with string values.
[TypeSharp] public enum UserRole { Guest, Member, Admin }
[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.
[TypeSharp] [Union] public enum UserRole { Guest, Member, Admin }
[TypeSharp] and [Union] can be combined in any order or syntax.
// 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 { ... }
Enum references in other decorated classes are resolved automatically.
export interface UserDto { id: number; name: string; role: UserRole; // resolved enum reference }
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);
| Feature | [TypeSharp] | [TypeSharp][Union] |
|---|---|---|
| Output | export enum | export const … as const |
| Type | enum type | literal union type |
| Value access | UserRole.Admin | UserRole.Admin |
| Discriminated unions | Less ideal | ✅ Ideal |
| Tree-shakeable | Depends on bundler | ✅ Yes |