Skip to content

siyavuyachagi/TypeSharp.Attributes

TypeSharp.Attributes

NuGet version NuGet downloads License: MIT GitHub stars Sponsor

C# attributes for controlling TypeSharp TypeScript generation.

TypeSharp is a direct C# → TypeScript type generator. This package provides the attributes that let you fine-tune what gets generated and how — without touching the CLI config.


Installation

dotnet add package TypeSharp.Attributes

Attributes

Attribute Target Description
[TypeSharp] (Class/Enum) Marks a class or enum to be included in TypeScript generation
[TypeIgnore] Property Excludes the property from TypeScript output
[TypeName("...")] Property Overrides the property name in the generated TypeScript
[TypeAs("...")] Property Overrides the inferred TypeScript type
[Obsolete("...")] Property Marks a property as obsolete and optionally affects TypeScript type output

Usage

[TypeIgnore]

Exclude a property entirely from the generated output.

using TypeSharp.Attributes;

[TypeSharp]
public class UserDto
{
    public string Email { get; set; }

    [TypeIgnore]
    public string PasswordHash { get; set; }
}

Generated TypeScript:

export interface UserDto {
  email: string;
}

[TypeName("...")]

Override the property name in the generated TypeScript output.

[TypeSharp]
public class UserDto
{
    [TypeName("created_at")]
    public DateTime CreatedAt { get; set; }

    [TypeName("updated_at")]
    public DateTime UpdatedAt { get; set; }
}

Generated TypeScript:

export interface UserDto {
  created_at: string;
  updated_at: string;
}

[TypeAs("...")]

Override the inferred TypeScript type for a property.

[TypeSharp]
public class UserDto
{
    [TypeAs("Date")]
    public DateTime CreatedAt { get; set; }

    [TypeAs("'admin' | 'user' | 'guest'")]
    public string Role { get; set; }
}

Generated TypeScript:

export interface UserDto {
  createdAt: Date;
  role: "admin" | "user" | "guest";
}

Combining attributes

All three attributes can be stacked together or alongside [Obsolete].

[TypeSharp]
public class ProductDto
{
    public Guid Id { get; set; }

    [TypeName("display_name")]
    public string Name { get; set; }

    [TypeAs("'active' | 'inactive'")]
    public string Status { get; set; }

    [TypeIgnore]
    public string InternalCode { get; set; }

    [Obsolete("Use Status instead.")]
    public bool IsActive { get; set; }
}

Generated TypeScript:

export interface ProductDto {
  id: string;
  display_name: string;
  status: "active" | "inactive";
  /** @deprecated Use Status instead. */
  isActive: boolean;
}

Requirements


Related


License

MIT © Siyavuya Chagi


Built with ❤️ in South Africa 🇿🇦

About

C# attributes for controlling TypeSharp TypeScript generation — fine-tune property names, types, and visibility without touching the CLI config.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors

Languages