Comments (12)
I don't know if you want to go down this path but here is how I solved things like this.
First, I will say that I already flag the types I want to export with an attribut called "ExportToTypescript", so our solution was already prepared to use attributes.
We ran into a case where we really wanted some strings to be nullable and we are stuck on C# 7.3 for the moment, so we couldn't adopt string? as the type. So we introduced another attribute called [TypescriptType(type= "string | null")].
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class TypescriptType: Attribute
{
public string Type { get; set; }
public override string ToString()
{
return Type;
}
}
Then I use it like this:
[ExportToTypescript]
public class CartCheckoutInfo
{
[TypescriptType(Type = "string | null")]
public string Message { get; set; }
public string PaymentId { get; set; }
}
And to extract the "string | null",
public static string ToTypeScriptType(IProperty prop)
{
var attr = prop.Attributes.FirstOrDefault(a => a.Name == "TypescriptType") ?? prop.Type.Attributes.FirstOrDefault(a => a.Name == "TypescriptType");
var attrType = attr?.Arguments.FirstOrDefault()?.Value;
if (attrType != null) return attrType as string;
return prop.Type.ToTypeScriptType();
}
I know this is more manual, but it gives you the ultimate control over the type exported.
from ntypewriter.
I will think about it. I have just released version v0.3.6 which contains a commit that exposes IType.IsTuple, that should help eliminate that ugly string comparison.
from ntypewriter.
It was not, @Prinsn proposed adding ValueTupleTypesScriptType
parameter, and I only commented on his proposal, without making any remarks regarding named tuples,
from ntypewriter.
Having copied the TypeScriptionFunctions
locally, appending to TranslateNameToTypeScriptName
if (type.Name.StartsWith("("))
return "ValueTuple";
Appears to work in this case, so whatever analysis would be used to determine it exists seems sufficient.
from ntypewriter.
That does seem like it'll be worth keeping in mind, though the only type that seems to have given us any problems currently is this specific case.
We've not run into any issues with your cited case, however, with Typewriter definitely not emitting | null
I was actually surprised to see it pop up in NTypewriter generation since it is more correct.
from ntypewriter.
The other place where we used it was for a DBId class we have.
We return hashed strings for the Id field of a db record, but we have a class that fairly seemlessly translates between the string Id and the numeric Id from the database. We used this attribute to have NTypewriter output a string type for this DBId type.
We haven't run across any other use cases yet, and we wouldn't have needed it if we could move up to a newer version of C# that allows for nullable types.
from ntypewriter.
I'm really not sure what you would see as optimal, but since ValueTuple is a type that serializes just fine, but has no native type, it seems like the most straightforward thing would be a configuration parameter for ValueTupleTypesScriptType
that gets utilized pulled there?
I don't know if there are any such similar types you'd have to handle for.
from ntypewriter.
from ntypewriter.
I do not think that we need a parameter for that, ValueTuple
can be hard coded, it is the name of type that the compiler uses internally anyway.
@Prinsn you are welcome to prepare PR that adds support for ValueTuples to ToTypeScriptType. Do not forget to write tests.
from ntypewriter.
@NeVeSpl I am not sure if the comment of "I do not think we need a parameter for that" was aimed at my comment about named tuples. If it was, then I think we are miscommunicating.
Named tuples allows you to write:
(string msg, boolean flag) SomeFunction() {}
This should serialize to
{msg: string, flag: bool} function SomeFunction() {}
instead of
{item1: string, item2: bool} function SomeFunction() {}
from ntypewriter.
from ntypewriter.
Right now it does not generate valid TS for tuples either, so having a type name there would definitely be a step forward. But since it is not a very popular thing, it can stay as it is, your choice.
from ntypewriter.
Related Issues (20)
- Changes from GIT not picked up by NTypewriter HOT 3
- This is perfect for AI
- Failed to compile user code (System.Collections) HOT 10
- Source Generator ArgumentNullException HOT 8
- Save function is misleading HOT 3
- `Microsoft.CodeAnalysis` not found HOT 10
- SourceGenerator can't find dependent projects HOT 2
- Publish latest releases as NuGet package HOT 4
- Template output preview is always displayed on top of everything HOT 1
- Template output preview with dark mode support
- Using C# 9.0 init accessors in a custom function gives an error inside Visual Studio extension HOT 1
- Extend AllReferencedTypes to search on Interfaces HOT 1
- Request - Property internal set vs public flag or filter HOT 3
- Action.Url does not support array-valued parameters HOT 1
- How to use builtin function LINQ.Where in template? HOT 7
- error while generating typescript services from ntypewriter runtime generator HOT 2
- Generic class with itself as argument of base type equality problem HOT 3
- How to join arrays without losing type information? HOT 2
- How to automatically render templates? HOT 2
- Incremental Generator HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ntypewriter.