frhagn / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
536 stars 132 forks source link

Import Custom Types from Dictionary? #282

Closed colinwurtz closed 6 years ago

colinwurtz commented 6 years ago

First of all, thanks for the awesome work on this, Typewriter has saved me an immense amount of time and effort.

I used the technique outlined in #166 to generate imports for custom types, but am struggling to figure out how to generate my imports line for a Dictionary<int,customtype> scenario.

My DTO/C# class is defined below. The Imports function in my .tst file is not picking up that it needs to import the Status type, similar to how it imported ProjectDTO.

[TypeScript]
public class ActiveProjectsViewModel
{
    public List<ProjectDTO> Projects { get; set; }

    public Dictionary<int,Status> Statuses { get; set; }

    public bool EditModeAvailable { get; set; }
}

tst file

 ${
string Imports(Class c)
        {
            var props = c.Properties.Where(p=>!p.Attributes.Any(a => String.Equals(a.name, "TypeScriptIgnore", StringComparison.OrdinalIgnoreCase)));

            IEnumerable<Type> types = props
                .Select(p => p.Type)
                .Where(t => !t.IsPrimitive || t.IsEnum)
                .Select(t => t.IsGeneric ? t.TypeArguments.First() : t)
                .Where(t => !t.Attributes.Any(a => String.Equals(a.name, "TypeScriptIgnore", StringComparison.OrdinalIgnoreCase)))
                .Distinct();

            return string.Join(Environment.NewLine, types.Select(t => $"import {{ {t.Name} }} from './{t.Name}';").Distinct());
        }
    }

    $Classes()[
    $Imports

    export class $Name {
        $Properties($PropertyIsNotIgnored)[        
        public $name: $Type = $Type[$Default];]
        $BaseClass[$Properties($PropertyIsNotIgnored)[
        public $name: $Type = $Type[$Default];]]
    }]

    $Enums(*)[
    export enum $Name {
        $Values[
        $Name = $Value][,]
    }]

Produces this file:

import { ProjectDTO } from './ProjectDTO';

export class ActiveProjectsViewModel {

    public projects: ProjectDTO[] = [];        

    public statuses: { [key: number]: Status; } = {};        

    public editModeAvailable: boolean = false;

}

Is there a way to get to the type of the dictionary's values within the .tst file?

Cross posted to stack overflow here: https://stackoverflow.com/questions/52322098/how-do-i-import-types-defined-in-a-c-sharp-dictionary

frhagn commented 6 years ago

Hi @colinwurtz, Try something like this:

string Imports(Class c)
{
    var props = c.Properties.Where(p => !p.Attributes.Any(a => String.Equals(a.name, "TypeScriptIgnore", StringComparison.OrdinalIgnoreCase)));

    IEnumerable<Type> types = props
        .Select(p => p.Type)
        .SelectMany(t => t.IsGeneric ? t.TypeArguments : new[] { t } as IEnumerable<Type>)
        .Where(t => !t.IsPrimitive || t.IsEnum)
        .Where(t => !t.Attributes.Any(a => String.Equals(a.name, "TypeScriptIgnore", StringComparison.OrdinalIgnoreCase)))
        .Distinct();

    return string.Join(Environment.NewLine, types.Select(t => $"import {{ {t.Name} }} from './{t.Name}';").Distinct());
}
colinwurtz commented 6 years ago

That worked... thank you!