CSharpToTypeScript
CSharpToTypeScript copied to clipboard
Fixed string to constant
First of all, thanks for the work put in here 🎉
Just one nuance I came to notice when I wanted to use it in my project: In my codebase in C# I have two types (tracks and albums) which both can be in a list (as IDocuments). Both have the property type
on which they both have a fixed name.
By this I can have a union-type in typescript but am able to determine by the property type
which of them I have at hand, and the typescript compiler will narrow down the type. This is not possible if I want to use this repo to convert my C# classes to typescript, because it translates the property type
to string
.
Input
public interface IDocument : IDocument<int>
{ }
public interface IDocument<T>
{
T Id { get; set; }
string Type { get; }
}
public class Album : IDocument
{
public static string TypeName = "album";
public int Id { get; set; }
public IEnumerable<Track> Tracks { get; set; }
public string Type => TypeName;
}
public class Track : IDocument
{
public static string TypeName = "track";
public int Id { get; set; }
public int Length { get; set; }
public string Type => TypeName;
}
Current output
export interface Document extends Document<number> {
}
export interface Document<T> {
id: T;
type: string;
}
export interface Album {
id: number;
tracks: Track[];
type: string;
}
export interface Track {
id: number;
length: number;
type: string;
}
Expected output
export interface Document extends Document<number> {
}
export interface Document<T> {
id: T;
type: string;
}
export interface Album {
id: number;
tracks: Track[];
type: "album";
}
export interface Track {
id: number;
length: number;
type: "track";
}
By the expected output, this typescript code would be valid:
(foo: Album | Track) => {
if (foo.type === "track") {
foo.length
}
};