question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional properties are ignored

See original GitHub issue

The following code in C # …

public int? ParentId { get; set; }

… results in the following in Typescript …

parentId: number;

… while the correct one should be:

parentId?: number;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
johnknoopcommented, May 2, 2020

Sorry for hijacking the thread, but I’m looking to replace TypeWriter because it doesn’t handle nullable reference types in C#. Judging by this thread, TypeGen doesn’t automatically translate C# nullables into TS optionals. Is it a simple thing to configure it to do that?

0reactions
elwynelwyncommented, Mar 22, 2022

Also bumping old thread sorry^^

Would love a way to make C# nullable types optional TS properties. I’ve set up csNullableTranslation to undefined which is correctly generating the union type T | undefined, however this means you still have to manually specify these properties as undefined when creating these objects.

class Car {
    string Model { get; set; }
    Sunroof?: Sunroof { get; set; }
}

becomes

interface Car {
    model: string;
    sunroof: Sunroof | undefined;
}

and now for ever car I define, I must do

const car: Car = {
    model: 'some cool car',
    sunroof: undefined
};

whereas I would prefer to define all cars without sunroofs like:

const car: Car = {
    model: 'some cool car'
};

(imagine there are many of these properties which can optionally be used in different situations, and currently I have to set every one of them as undefined).

I’d prefer not to instrument many many C# classes / properties with TypeGen attributes, and instead base it from our existing nullable properties. (I did try the attribute but it didn’t seem to actually work - perhaps because I am using a GenerationSpec? I’m also unable/unwilling to manually set properties as optional in the spec due to there being way too many / not wanting a giant blog of config)

Any other workarounds to suggest? EDIT: and if this isn’t possible, is this something you would be open to supporting if I put together a PR? (either as a new config option, or a new value for csNullableTranslation)

Read more comments on GitHub >

github_iconTop Results From Across the Web

In typescript, in a typed function argument, optional ...
I'm trying to create a router initialiser with a nice typed DX in typescript. My goal is to allow the developer to define...
Read more >
Optional properties should be ignored when inferring K in ...
#14295 and Pick turns optional parameters into required ones when union types are used #20722 look similar but appear to be about Pick...
Read more >
How to ignore properties with System.Text.Json
To ignore individual properties, use the [JsonIgnore] attribute. The following example shows a type to serialize. It also shows the JSON output:.
Read more >
Entity Properties - EF Core
A property is considered optional if it is valid for it to contain null . If null is not a valid value to...
Read more >
Optional property declarations should not use both '?' and ' ...
Optional property declarations should not use both '?' and 'undefined' syntax ... Use optional property syntax for properties holding some additional information.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found