svenheden / csharp-models-to-typescript

C# models to TypeScript
89 stars 58 forks source link

Ability to choose between a declared module or an exported module when generating types #20

Open SafeerH opened 5 years ago

SafeerH commented 5 years ago

Problem

I used to access my enum values (in a type-safe way) from my Angular view whenever I needed, by exposing it from my component as below.

E.g.

// my-enum.ts
export enum MyEnum {
  Ebay = 'Ebay',
  Amazon = 'Amazon',
}

// my-component.ts
import { MyEnum } from 'my-enum';

export class MyComponent {
  MyEnum: typeof MyEnum = MyEnum;
}

Now I'm running into to a problem where I can't access my generated enum values from my Angular view as I used to do because of the generated enums are defined under a declared module.

// my-component.ts
export class MyComponent {
  MyEnum: typeof ApiModel.MyEnum = ApiModel.MyEnum;
}
// ERROR: **ReferenceError: ApiModel is not defined**

Solution

The solution for this problem would be to generate types (e.g. enums) under an exported module (instead of a declared module as the declare keyword will not generate any JS code and only being used by the TS compiler)

So, it would be nice to have an option in the config file to choose between a declared module (e.g. model.d.ts) or an exported module (e.g. model.ts) when generating types.

svenheden commented 5 years ago

That's a great suggestion, feel free to do a PR! 🙏🏼

wzuqui commented 5 years ago

I had a problem like yours, my frontend project is angular and needed to export enum and classes, try my project, we can help each other.

https://github.com/svenheden/csharp-models-to-typescript/issues/38