svenheden / csharp-models-to-typescript

C# models to TypeScript
89 stars 58 forks source link

How to handle nullable reference types #52

Open thebuilder opened 3 years ago

thebuilder commented 3 years ago

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?

svenheden commented 3 years ago

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!