csharp-models-to-typescript
csharp-models-to-typescript copied to clipboard
Class objects should be converted to optional properties
Hi, thanks for this great tool ! I've got a question, maybe i'm missing something.
Let's say I've this 2 classes :
class myClass1 {
int prop1
}
class myClass2 {
public MyClass1 obj ;
}
shouldn't this be converted to :
interface myClass1 {
prop1: number
}
interface myClass2 {
obj?: myClass1
}
I'm asking this because of TS2322 i'm not able to set non-optional properties to undefined.
Hi, I'm glad you like the tool! Yes it would result in those interfaces, if it wasn't for the fact that empty classes are excluded.
My classes aren't empty (I updated the simplified example), but I'm getting non optional properties. Am I maybe missing something ?
Maybe you can instantiate your MyClass2
this way?
Note the difference between this one
Yes that works of course.
But I can set myClass2.obj to undefined afterwards. myClass2.obj = undefined
triggers TS2322 when the property is not optional while that would possible in C#.
Therefore classes should be exported as optionals (this includes strings)