Creating non-ambient modules with implementation


the TypeScript DTO generator currently creates an ambient module which can be used to obtain typing information. While this is great for many cases, its utility is somehow constrained for the following two reasons:

  • The ambient module is not compiled to JavaScript. Enums that are used in DTOs will not be converted to JavaScript Objects and are therefore not usable (e.g. for assignment). We are currently getting around this by manually changing the ambient module to a real module, exporting all the interfaces and enums that we need and compiling it to JavaScript. Obviously this is not perfect, because we have to do that every time the contracts change.
  • The ambient module only contains interfaces. If we want to new-up a DTO in our code it is not possible with just Interfaces. We have to create a new object and “cast” it to the respective interface.

It would be great if it was possible to generate a TypeScript Module that contained implementations instead of interfaces. With the current approach we have to do manual post-processing of the generated DTOs, while it would be great to just use the generated code without any modifications.

Thanks for helping us out

1 Like

The beauty of TypeScript interfaces are that it’s just optional type info used to aid with type safety that gets erased at runtime that lets you annotate the same untyped JS object returned by JSON.parse().

Can you submit a new Feature Request that describes the use-case you prefer the TypeScript implementations for, i.e. a simple example of what you’re doing now, are you manually parsing the JSON and converting object literals into concrete TypeScript classes?

I added a feature request:

1 Like

Hi, I’ve added support for this in the latest v4.0.47 on MyGet with the new ExportAsTypes=true option where it will create and export classes instead of interfaces. You can see the latest version of this in the latest Test site:

1 Like

Wow, that was quick. I’m deeply impressed.

I went through the generated code. It looks perfect. The only thing, that needs to be removed is the “declare” keyword in front of the module, otherwise it is still an ambient module.

Thanks a lot for reacting so quickly.

thx for the feedback, this is now added in:

Which is a short-cut for enabling the ExportAsTypes=true option, e.g:

We’ve also added and index page with links to the different languages:

And available from v4.0.47 on MyGet, if you had a previous v4.0.47 installed you’ll need to clear your NuGet packages cache.