csharp-models-to-typescript icon indicating copy to clipboard operation
csharp-models-to-typescript copied to clipboard

How to handle nullable reference types

Open thebuilder opened this issue 3 years ago • 1 comments

Hey, really cool tool. Tried it on on set of c# code models we have to compare it to our TypeScript models. This does reveal one issue - some types like string could be nullable by default. https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references

An example would be the following conversion:

public class LinkViewModel {
  public Href { get; set; }
  public string Label { get; set; }
  public string Title { get; set; }
  public LinkTargetTypes? Target { get; set; }
}
export interface LinkViewModel {
  href: string;
  label: string;
  title: string;
  target?: LinkTargetTypes;
}

The problem is, these values could actually be null, or undefined (depending on how you ship the data). TypeScript would not throw an error if you try to use a string method on on any of the string values.

Would it make sense to be able to control how reference types are handled?

thebuilder avatar Apr 16 '21 13:04 thebuilder

Would it make sense to be able to control how reference types are handled?

Yeah I think that's a great idea. If you're up for it feel free to submit a PR for adding a setting like this.

Thanks for the suggestion!

svenheden avatar Apr 21 '21 06:04 svenheden