cskeppstedt / t4ts Goto Github PK
View Code? Open in Web Editor NEWT4 template to generate TypeScript interface definitions.
License: Apache License 2.0
T4 template to generate TypeScript interface definitions.
License: Apache License 2.0
The t4 script should pull the inherited properties to complete the object.
Quite an easy suggestion but generally all interfaces are prefixed with 'I'.
The current implementation is perfect for getting data because you are just using the interface, but to support CRUD you need to be able to instantiate a class.
So how about rather than exporting interfaces you export classes and generate just a .ts file or also create the class that implements the interface?
..unless I've missed something?
Hello again..
Still loving the code!
So I have a BaseModel which all my models inherit from. When T4TS generates the interfaces it currently doesn't add the 'extends BaseModel' to the interface declaration.
Is it possible to do this (inferring from class definition)?
I hacked the code as I know my base type..
<#= module.IsGlobal?"":"export "#>interface <#= tsInterface.Name + (tsInterface.Name == "BaseModel" ? "" : " extends BaseModel") #> {
Thanks again.
Any plans to support .NET Core?
I made some very simple modifications to the T4TS.tt file in my solution so the generated enties better support BreezeJS. You can see my full T4TS file at https://gist.github.com/alexdresko/5393155 and a potentially helpful explanation for my madness at http://stackoverflow.com/questions/13817230/breeze-js-typed-entities/16028385#16028385.
I'm interested to know what you think about this idea as being an actual feature of T4TS, perhaps by way of an additional attribute like TypeScriptInterfaceAttribute.. maybe TypeScriptBreezeInterfaceAttribute?
pls add this feature
Please excuse my ignorance. I've very new to T4. I was able to modify the project and verify that my modifications work in T4TS.Example project. But how do I get the .tt file updated that is in the build folder? Are there some utilities or commands that I don't see?
Thanks,
Shane
At first, I have to say that t4ts is really a practical thing. I use it to automatically generate TS interfaces from my Entity Framework classes.
However, I found a little bug. If you have a partial class and you add [T4TS.TypeScriptInterface] to both implementations, you get this exception:
Error 19 Running transformation: System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary
2.Add(TKey key, TValue value)
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.TypeContext.AddCustomType(String typeFullName, CustomType customType) in \T4TS.tt:line 322
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.CodeTraverser.<>c__DisplayClass3.b__2(CodeClass codeClass) in \T4TS.tt:line 100
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.NamespaceTraverser.Traverse(CodeElements members) in \T4TS.tt:line 735
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.NamespaceTraverser..ctor(CodeNamespace ns, Action1 withCodeClass) in \T4TS.tt:line 729 at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.CodeTraverser.<>c__DisplayClass3.<BuildContext>b__1(CodeNamespace ns) in \T4TS.tt:line 91 at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.ProjectTraverser.Traverse(ProjectItems items) in c:\Projects\P0900 BR Bauhandel\MasterDataManager\T4TS.tt:line 764 at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.ProjectTraverser.Traverse(ProjectItems items) in \T4TS.tt:line 768 at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.ProjectTraverser..ctor(Project project, Action
1 withNamespace) in \T4TS.tt:line 753
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.CodeTraverser.BuildContext() in \T4TS.tt:line 89
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.CodeTraverser.GetAllInterfaces() in \T4TS.tt:line 109
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.GetDataToRender() in \T4TS.tt:line 45
at Microsoft.VisualStudio.TextTemplating7B90580D93E684080705DF19CF6B1B1F04EA4B8FB10D9183C028C26006ED272AA3BCA78DE743602F8886F86B5820A9085A4D4B820EA3F5B48100BC224FE920EE.GeneratedTextTransformation.TransformText() in \T4TS.tt:line 14 T4TS.tt 322 1
Given the following file:
[TypeScriptInterface]
public class ChildClass : ParentClass
{
public string ChildProperty { get; set; }
}
[TypeScriptInterface]
public class ParentClass : GrandparentClass
{
public string ParentProperty { get; set; }
}
[TypeScriptInterface]
public class GrandparentClass
{
public string GrandparentProperty { get; set; }
}
The generated TypeScript file extends both ParentClass and ChildClass from GrandparentClass. Correct behaviour should be to extend ChildClass from ParentClass.
If GrandparentClass is declared at the top of the file, the correct behaviour is observed. This becomes a problem when working across multiple files as there is no way to ensure the correct declaration order.
A potential fix has been submitted. (Sorry, couldn't work out how to attach pull request to this issue).
This tool saves time and is worth using, but doesn't handle IEnumerable, enum, uses bool instead of boolean, etc. In my case I'm using it for a one-time generation of interfaces so this is not a problem but generating t.ts files from C# is a great service that deserves support.
Are there plans to maintain this tool or any newer replacements?
Currently you have to specify nullable fields by decorating the property -
[TypeScriptMember(Optional = true, Type = "bool")]
public bool? IsMOJ { get; set; }
Is it possible to infer whether its optional from the nullable 'bool?'
Also when i declared my bool as nullable the output in the d.ts file set its type as 'any' and lost its actual type.
For people like me, just add the following code on line 993 :)
case "System.DateTime":
case "System.DateTimeOffset":
if (Settings.UseNativeDates)
return new DateTimeType();
else
return new StringType();
I tried to generate nested classes interfaces but apparently it does not work - even after adding [TypeScriptInterfaceAttribute] to the inner classes.
Would it be hard to change the code to make this available?
I would be available to change the code.
If I augment my model in a partial class with a set of properties it does not generate the new properties.
We use servicestack and its JSON serializer converts all POCO property names to camelCase.
Could we have an option to get T4TS to do this also?
Hi!
Currently the TypeScriptInterfaceAttribute
is defined only for C# classes AttributeTargets.Class
. Have you checked, is there anything that would make it difficult to change it to allow interfaces as well?
As the subject says, an option in the settings file to specify where the T4TS.d.ts file is placed. I like to keep the root clean. I'm not sure its possible since most T4 is always placed in the root folder.
I guess I could also just move the T4TS.tt file.
My solution consists of one assembly containing T4TS.tt and some classes decorated with TypeScriptInterface which in turn reference some types in a depended assembly which just references the T4TS attributes assembly. Unfortunately all references to types in the depended assembly resolve to type "any" when compiling T4TS.tt.
Hi,
It a great job! It saves a lot of typing time! Thanks for creating it! :)
I have two questions:
Thanks,
András
Once again, I have discovered something in which T4TS can be improved with a single line of code :)
If you have a property in c# prefixed with "@" (can be done to make properties have the name of a keyword), T4TS generates an invalid TypeScript file. In my case I had a propery named readonly in C# which resulted in a Syntax Error in TS. To fix this, just change line Line 143 to:
member.Name.StartsWith("@")? member.Name.Substring(1): member.Name
I think IDicionary<string, object> should be outputed as { [name: string]: any} as this is what Newtonsoft.Json does when serializing .Net Objects to JSON
This is what my pull request #46 is for
Hello
Great work by the way..
So my models live in a separate assembly to my T4TS template. The current implementation doesn't allow me to specify which assembly / assemblies I want to get my models from.
I hacked the code and changed the following in the GetDataToRender method..
// var generator = new CodeGenerator(project, settings);
// return generator.GetAllInterfaces().ToList();
var interfaces = new List<TypeScriptModule>();
foreach (Project proj in dte.Solution)
{
if(proj.Name == "XYZ")
{
var generator = new CodeGenerator(proj, settings);
interfaces.AddRange(generator.GetAllInterfaces().ToList());
}
}
return interfaces;
}
Is it possible to get functionality like this in your code? Its a good feature request!
TypeScript 1.5 introduced the declare namespace
syntax as an alias for the declare module
syntax in order to disambiguate external and internal modules. The recommended practice is to avoid the old declare module
when declaring an what were called internal modules and are now called namespaces in TypeScript terminology. See microsoft/TypeScript/pull/2923 where Anders Hejlsberg explains the change.
Running the t4ts.tt crashes VS 2015.
[TypeScriptEnum]
public enum Make
{
Ford,
Tesla,
Mercedes
}
With the 0.9 release of TypeScript just around the corner is there a plan to add support for Generics into the T4TS templates?
Would it be possible to add an option to generate typescript classes instead of interfaces?
I have a C# class with a large number of attributes that I need to have a typescript object for. I really defeats the purpose of a generated class if I have to manually implement the interface.
Thanks
Joe
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.