变体类型和非,同名

Variant type and non, with same name

我试过将 typelite 与 entity framework 和 Identity framework 一起使用。 在 identity framework 中存在格式为 TypeName<Tkey>TypeName: TypeName<String> 的各种类型。 typelite 正确地导出了类型,但这种行为在 typescript 中是不可能的,我应该如何解决这个问题?

Class:

namespace DTO
{
    [TypeLite.TsClass]
    public class Test : Test<String>
    {
    }

    public class Test<TKey>
    {
        public TKey Id { get; set; }
        public String Name { get; set; }
    }
}

配置 1:

<# var ts = TypeScript.Definitions()
    .WithReference("Enums.ts")
    .ForLoadedAssemblies();
#>

输出1,这里的错误是:Type 'Test<TKey>' recursively references itself as a base type.

declare module DTO {
    interface Test extends DTO.Test<string> {
    }
    interface Test<TKey> {
        Id: TKey;
        Name: string;
    }
}

配置 2:

<# var ts = TypeScript.Definitions()
    .WithReference("Enums.ts")
    .ForLoadedAssemblies()
    .WithTypeFormatter((type, f) => "I" + ((TypeLite.TsModels.TsClass)type).Name);
#>

输出 2,这里变体 Type 也得到了 I,事情变得一团糟

declare module DTO {
    interface IUser {
        Id: string;
        Name: string;
        Surname: string;
        UserName: string;
        Email: string;
        Roles: string[];
    }
    interface ITest extends DTO.ITest {
    }
    interface ITest {
        Id: ITKey;
        Name: string;
    }
}

如果您在同一个命名空间中同时拥有同名的泛型和非泛型类型,则必须为它们指定一个稍微不同的名称。

将模型定义更改为:

var ts = TypeScript.Definitions()
    .WithReference("Enums.ts")
    .ForLoadedAssemblies();
ts.WithTypeFormatter((t, f) => GenerateTypeName(t, f, ts.ScriptGenerator));

在最后添加:

<#+
    private string GenerateTypeName(TsType tsType, ITsTypeFormatter f, TsGenerator gen)
    {
        TsClass tsClass = (TsClass) tsType;

        string name = tsClass.Name;
        if (!tsClass.GenericArguments.Any())
            return name;

        name += "_" + tsClass.GenericArguments.Count;

        name += "<" + string.Join(",", tsClass.GenericArguments.Select(arg =>
        {
            string suffix = "";
            var argCol = arg as TsCollection;
            if (argCol != null)
            {
                suffix = string.Join("", Enumerable.Repeat("[]", argCol.Dimension));
            }
            return gen.GetFullyQualifiedTypeName(arg) + suffix;
        })) + ">";

        return name;
    }
#>

它将生成:

declare module DTO {
    interface ITest extends DTO.ITest_1<string> {
    }
    interface ITest_1<TKey> {
        Id: TKey;
        Value: string;
    }
}