-
Notifications
You must be signed in to change notification settings - Fork 83
Configuring multiple files export
You can spread your generated TypeScript code among several files. To do that, follow these steps:
Set RtDivideTypesAmongFiles
to True
in Reinforced.Typings.settings.xml.
Also set RtTargetDirectory
there to directory that you want to have your files in. You can use variables from MSBuild in Reinforced.Typings.settings.xml, so $(ProjectDir)\Scripts\MyApplication
will be valid value there.
Use fluent configuration's .Global method or [TsGlobal] attribute to set RootNamespace
property to your app's root namespace. This step is optional, but highly recommended to do. Because otherwise Reinforced.Typings will create separate directory inside RtTargetDirectory
for every "." in your namespaces.
If you prefer attributes, then navigate to your files and arrange [TsFile("filename.ts")]
attribute above them to make them generated to filename.ts
file. File extension is important here. File name must be specified relatively to your RtTargetDirectory
.
If you use fluent configuration, then you can use .ExportTo
method to specify file for particular set of types:
builder.ExportAsInterface<MyModel1>().WithPublicProperties().ExportTo("Models.ts");
var myModels = new[] { /* put your types here*/ };
builder.ExportAsInterfaces(myModels, x =>
x.WithPublicProperties()
.ExportTo("Models.ts"));
var myControllers = new [] { /* put your types here*/ };
builder.ExportAsClasses(myControllers, x=>
x.WithPublicProperties()
.WithPublicMethods()
.ExportTo("Controllers.ts"));
Rules regarding file name are basically the same.
This step is also optional, but by default Reinforced.Typings will export each class to separate file.
Now you can rebuild your project and include generated files to solution. That's all.
Please do not care about references in generated files manually. Reinforced.Typings will automatically generate valid <reference ...>
s (or import
s in case of using modules) directives in each exported file. Normally you do not have to do anything extra to arrange references. Regardless of files number. Anyway, Reinforced.Typings cannot handle cyclomatic references automatically, you it is entirely up to you to build your files structure in way to do not make it cyclomatic-referenced.
- If you need some exported type to reference specific file (3rd party library, typings or non-generated source files), then use
[TsAddTypeReference("path_to_file.ts")]
above your type to notify Reinforced.Typings to include this<refernece ...>
to file, that will contain this type (it is much easier to use than to explain) - Also you can use
[TsAddTypeReference(typeof(AnotherExportedType))]
supplying another exported type to it. Use this approach to add reference to file containingAnotherExportedType
to another file. It can be used if for some reason Reinforced.Typings will not generate reference to proper type. Works exactly as[TsAddTypeReference]
with string path, but computes path automatically -
[TsAddTypeReference]
has "global twin" -[assembly:TsReference("path_to_reference.ts")]
. You can use this approach to add reference to each generated file. E.g. you can reference 3rd party libraries globally using this approach - The same functionality is exposed by fluent
.AddReference
methods ofConfigurationBuilder
and fluent type configuration builders
If you've switched Reinforced.Typings to use modules either by calling .Global(x=>x.UseModules())
using fluent configuration or using [assembly:TsGlobal(UseModules = true, DiscardNamespacesWhenUsingModules = true)]
from attribute configuration then Reinforced.Typings will generate import
directives insteaf of <refernece ...>
. Explicitly added references meanwhile will stay the same.
Important! Imports will not appear at all in your code until you enable modules usage!
There are several additional ways to operate imports:
- If you need
[TsAddTypeReference]
functionality, but for imports and in case of using modules - then use[TsAddTypeImport]
- For global imports use
[TsImport]
attribute - Functionality for manipulating imports from fluent configuration is exposed by
.AddImport
fluent methods - Reinforced.Typings generates single import per type:
import { SomeNeededType } from './SomeFile'
. If your file contains too much single-type references to single module then you can "help" Reinforced.Typings by adding wildcard import to one of types in file. This case is considered in ReferencesPart4 unit test - If you will instruct Reinforced.Typings to discard references but still using modules then wildcard imports will be generated in each files:
import * as File from './File'
. In this case, types placed in namespaces will be referenced in code asFile.Name.Space.Type
whereFile
is wildcard import alias
Multiple files export and references are extremely important topics and are considered in Reinforced.Typings References tests. Please check them to catch the point and get more additional information: