Home

Awesome

NTypewriter

ci Visual Studio Marketplace Version Nuget Nuget

NTypewriter LivePreview

<h3 align="center"> Scriban templates + Roslyn C# code model => generated files </h3> <h4 align="center"> design/compile/run time == any time </h4>

<ins>For those who do not know Typewriter</ins>:

NTypewriter is files generator from text templates populated with meta-data about your C# code. It is like a specialized and more convenient T4 design-time template.

With NTypewriter you can:

NTypewriter comes in many flavours, that can be used according to your needs:

more about NTypewriter architecture and all extension points that can be used, you will find here

<ins>For those who know Typewriter</ins>:

NTypewriter is a younger and more immature brother of beloved Typewriter. They share the same ideas but with a completely different implementation. NTypwriter uses Scriban as a template engine, thus template files are completely not interchangeable. While code model API is about 95% compatible between them, there are some differences. NTypewriter code model is 100% pure, without any amenities that help generate TS files. All things that help generate TypeScript from ASP.NET are located in built-in functions: Action, Type.

Oh, did I forget to mention that NTypewriter also solves most of the awaited issues of the Typewriter that were promised for 2.0 version:

Index

Typewriter vs NTypewriter

 TypewriterNTypewriter
Template file extension*.tst*.nt
Syntaxtypewriter syntaxscriban scripting language
Lambda filterspresentyes
Can be used from CLInoyes
Can be used in pipelinenoyes
Full control over whitespacesnopeyup
Mappingone input always produces one output fileyou can generate as many files as you want
Live previewnoyes
Code model
Unit of workfilethere is no concept of a file in NTypewriter, you work on compiled symbols
Access modifierscode model contains only public typescode model contains all types
Partial classestreated as separate unitsall parts of the class are treated as a whole unit
Automation
Auto-render template on saveyes (opt-out is possible)yes (opt-in is possible)
Auto-render when C# file changesyes (opt-out is possible)no
Auto-render on buildnoyes (opt-in is possible)
Custom functions
Placementinside template file (.tst)in separate file (*.nt.cs)
Can be sharedseparate for every templateshared between templates inside a project
Can be debugnoyes
Can be unit testednoyes
VS Integration
Supported versions of Visual Studio2015, 2017, 20192019 (min ver 16.11.x), 2022
Add generated files to VS projectyes (opt-out is possible)yes (opt-out is possible)
Sync deleted or renamed C# types with generated filesthere is a part of the code that should do that but it does not work anymoreyes (only when the above option is enabled)

Typewriter template:

module App { $Classes(*Model)[
    export class $Name { $Properties[
        public $name: $Type;]
    }]
}

equivalent NTypewriter template will be: (open in NTypewriter.Online)

{{- for class in data.Classes | Symbols.WhereNameEndsWith "Model"
        capture output -}}
module App {
    export class {{ class.Name }} {
            {{- for property in class.Properties | Symbols.ThatArePublic }}
        public {{ property.Name | String.ToCamelCase }}: {{ property.Type | Type.ToTypeScriptType }};
            {{- end }}
    }
}
    {{- end 
        filePath =  class.BareName | String.Append ".ts"
        Save output filePath
    end }}

yes, it is more verbose, but maintaining it over time will be much easier. Both templates generate exactly the same output:

module App {
    export class CustomerModel {
        public id: number;
        public name: string;
        public orders: OrderModel[];
    }
}

Examples

All Typewriter examples are available as .nt templates on github and also on NTypewriter.Online website.

Note nt. templates produce exactly the same output as .tst templates, even bad output formatting was preserved, to make them easier to compare.

exampleNTypewriterTypewriterOnline
CreateYourFirstTemplateCreateYourFirstTemplate.ntCreateYourFirstTemplate.tstopen
ExtensionsExtensions.ntExtensions.tstopen
ModelInterfacesModelInterfaces.ntModelInterfaces.tstopen
KnockoutModelsKnockoutModels.ntKnockoutModels.tstopen
AngularWebAPIServiceAngularWebAPIService.ntAngularWebAPIService.tstopen

Known issues

NTypewriter does not have own a lexer/parser as Typewriter has, and uses Scriban instead to do heavy work. Scriban works very well with fully correct templates, but with incomplete templates during editing not so much. It is the source of the most glitches in the Editor. Scriban language is also typeless, thus doing code completion is challenging.