Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed string to constant #63

Open
SimonSimCity opened this issue May 5, 2023 · 0 comments
Open

Fixed string to constant #63

SimonSimCity opened this issue May 5, 2023 · 0 comments

Comments

@SimonSimCity
Copy link

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
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant