Announcement

Collapse
No announcement yet.

[FIXED] [#3700,#3701,#3702] TypeScript generator - nullable/optional properties

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [FIXED] [#3700,#3701,#3702] TypeScript generator - nullable/optional properties

    Hi,

    Please consider the following simple example:
    https://deck.net/53f559f1b5c0a11285610206adc7602e

    I would expect the TypeScript definition to contain properties that are optional (both allowing Undefined and Null).

    We have some large objects with a lot of optional properties, and currently we are forced to initialize every single property when creating objects client side.

    How can I configure the compiler to make C# nullable types optional in TypeScript ?

    - Thanks


    Jimmy
    Last edited by fabricio.murta; 2018-09-12 @ 07:37 PM.

    #2
    To clarify what I would expect Bridge.NET to generate:

    declare namespace Demo {
        interface Test {
            Name?: string | null;
            Age: number;
        }
    }
    One could argue that the property is not optional on the server object and therefore should not be optional on the client. But server side the properties are initialized with Nulls so we dont have to set values - we don't have that advantage client side unless the properties are optional. It's really a pain to be forced to set 25 properties when we only need to use 5-6 of them.

    However, to ensure greater control and flexibility the best solution would probably be to default nullable property definitions to something like Prop: type | null, and allow us to set a compiler flag that makes individual properties optional, something like [Flags(optional)].
    Last edited by geoffrey.mcgill; 2018-09-04 @ 04:51 AM.

    Comment


      #3
      Actually I would personally prefer that Nullable properties default to being generated like so: Prop?: type | null.
      If one really don't want it optional, this should be something that can be disabled with a flag above each property, e.g. [Flags(required)]
      Last edited by geoffrey.mcgill; 2018-09-04 @ 04:53 AM.

      Comment


        #4
        Hi Jemt,

        Thanks for pointing out on that issue. I see it becomes important when strictNullChecks TypeScript compiler option is set.
        Let me split the problem into some smaller cases:

        1. C# nullable structs/primitives need to be translated into type | null:
        // C# declaration
        public class Person
        {
            public int? Age { get; set; }
        }
        
        // TypeScript
        ...
        interface Person {
           Age: number | null;
        }
        ...
        Even if we don't consider strictNullChecks, it'd be nice to emit number | null because in C# code nullable flag is stated explicitly.

        2. C# String primitive type is a bit more difficult to deal with. In C# it allows NULL assignments, but I feel the type string | null can be misleading in some cases in TypeScript. However, if we want to mimic C# behavior exactly, I'd say string | null would make emitted TS declaration more conforming with what we have in .NET.

        3. Continuing reasoning from the previous point - it looks like all reference types need to be emitted as type | null. Otherwise, strictNullChecks would suppress NULL assignments, whereas in .NET it's still allowed.

        4. TypeScript optional fields. Optional field allows undefined to be assigned, but this is what doesn't match to .NET behavior. Because of that, I'd not vote for emitting optional fields by default. I think this can be handled with Rules attribute, so anybody who needs that functionality would enable it by explicitly declaring that attribute or configuring it in bridge.json file.

        Comment


          #5
          I completely agree. Do I need to file an issue to have this fixed or have you already added it to your backlog ?
          Do you have an ETA? Will it be 1, 6, or 12 month into the future ?

          - Thanks

          Comment


            #6
            Hi. We are discussing the options today and should be able to provide some direction.

            Comment


              #7
              We've created the following three new Issues to cover the TypeScript nullable and optional type scenarios listed above.
              1. [#3700] emit | null in generated .d.ts for all C# Reference types
              2. [#3701] C# Nullable Types should emit | null in generated .d.ts file
              3. [#3702] Proposal: New [Optional] Attribute
              Issue #3702 regarding the [Optional] Attribute still requires some discussion on our side, so the proposal might be revised based on feedback.

              We don't have a timeframe for when these new features will be released, but they should be fairly simple to implement so shouldn't take long. At the moment I'm tagging the three issues for the Bridge 17.4.0 milestone. A delivery date is yet to be set on the milestone.

              Please review and provide any feedback.

              Hope this helps.

              Comment


                #8
                Thanks, much appreciated :-)

                Comment


                  #9
                  The three issues have been fixed and implemented (where it applies). The expected output will be available next Bridge release! At this time it is scheduled to be 17.4.0.

                  Comment


                    #10
                    Thanks, I appreciate that very much :-)

                    Comment

                    Working...
                    X