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

Class objects should be converted to optional properties

Open JeanMeche opened this issue 5 years ago • 5 comments

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.

JeanMeche avatar Jun 04 '19 08:06 JeanMeche

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.

svenheden avatar Jun 04 '19 11:06 svenheden

My classes aren't empty (I updated the simplified example), but I'm getting non optional properties. Am I maybe missing something ?

JeanMeche avatar Jun 04 '19 12:06 JeanMeche

Maybe you can instantiate your MyClass2 this way?

image

lacroixdavid1 avatar Jun 04 '19 13:06 lacroixdavid1

Note the difference between this one image

lacroixdavid1 avatar Jun 04 '19 13:06 lacroixdavid1

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)

JeanMeche avatar Jun 05 '19 08:06 JeanMeche