Code Monkey home page Code Monkey logo

Comments (12)

gregveres avatar gregveres commented on September 26, 2024 2

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.

NeVeSpl avatar NeVeSpl commented on September 26, 2024 1

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.

NeVeSpl avatar NeVeSpl commented on September 26, 2024 1

It was not, @Prinsn proposed adding ValueTupleTypesScriptType parameter, and I only commented on his proposal, without making any remarks regarding named tuples,

from ntypewriter.

Prinsn avatar Prinsn commented on September 26, 2024

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.

Prinsn avatar Prinsn commented on September 26, 2024

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.

gregveres avatar gregveres commented on September 26, 2024

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.

Prinsn avatar Prinsn commented on September 26, 2024

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.

gregveres avatar gregveres commented on September 26, 2024

from ntypewriter.

NeVeSpl avatar NeVeSpl commented on September 26, 2024

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.

gregveres avatar gregveres commented on September 26, 2024

@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.

Prinsn avatar Prinsn commented on September 26, 2024

from ntypewriter.

NeVeSpl avatar NeVeSpl commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.