jburzynski / TypeGen

Single-class-per-file C# to TypeScript generator
MIT License
195 stars 55 forks source link

Inheritance does not work when registration is done using AddInterface #170

Closed mstijak closed 1 year ago

mstijak commented 1 year ago

I have two classes registered using the AddInterface method.

AddInterface<DigitalIdentityContactMedium>();
AddInterface<EmailMedium>();

The EmailMedium class inherits DigitalIdentityContactMedium. The resulting code looks like this:

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

import { Credential } from "./Credential";

export interface LoginPasswordCredential {
    login: string;
    password: string;
}

The import is there, but the extends is lacking.

I believe the issue is in the TsContentGenerator.

public string GetExtendsForInterfacesText(Type type)
{
    Requires.NotNull(type, nameof(type));
    Requires.NotNull(GeneratorOptions.TypeNameConverters, nameof(GeneratorOptions.TypeNameConverters));

    IEnumerable<Type> baseTypes = _typeService.GetImplementedInterfaces(type);
    if (!baseTypes.Any()) return "";

    IEnumerable<string> baseTypeNames = baseTypes.Select(baseType => _typeService.GetTsTypeName(baseType, true));
    return _templateService.GetExtendsText(baseTypeNames);
}

This method looks only for the implemented interfaces and omits the base type. This used to work before. I'm not sure if registering DTO classes as TS interfaces is okay, though.

jburzynski commented 1 year ago

I've just added a test for TS interface inheritance, but it seems to be working - you can see src/TypeGen/TypeGen.IntegrationTest/TsInterfaceInheritance/TsInterfaceInheritanceTest.cs.

One thing I noticed is that the spec configures DigitalIdentityContactMedium and EmailMedium, but the TS file is for LoginPasswordCredential - is this just a typo, or can this be the reason for the issue?

mstijak commented 1 year ago

It's a dummy mistake from me, but the problem is actually there. It worked when types were added as classes instead of interfaces.

I'll check a bit more. Thank you for the response.

mstijak commented 1 year ago

For some reason, I cannot reproduce it anymore. Thank you.