RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.62k stars 1.23k forks source link

TypeScript interfaces should never reference classes #4932

Open varganatt opened 1 week ago

varganatt commented 1 week ago

NSwag.AspNetCore 14.0.8 Repo for reproduction: https://github.com/varganatt/NSwagRepro/blob/master/NSwagRepro.csproj Settings: nswag.json

When you have a type that contains nested data, and generate both TypeScript classes and interfaces, the interfaces reference nested class data.

Interfaces should never reference generated classes.


I have an endpoint that returns nested data (NestedData.cs):

public class NestedData
{
    public InnerDetails Details => new();

    public class InnerDetails
    {
        public int SomeValue { get; set; }
    }
}

From the generated TypeScript code:

export class NestedData implements INestedData {
    // Implementation
}

export interface INestedData {
    details?: InnerDetails; // <--- THE PROBLEM
}

export class InnerDetails implements IInnerDetails {
    // Implementation
}

export interface IInnerDetails {
    someValue?: number;
}

INestedData.details should be specified as the type IInnerDetails, not InnerDetails.