Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class objects should be converted to optional properties #21

Open
JeanMeche opened this issue Jun 4, 2019 · 5 comments
Open

Class objects should be converted to optional properties #21

JeanMeche opened this issue Jun 4, 2019 · 5 comments

Comments

@JeanMeche
Copy link

JeanMeche commented Jun 4, 2019

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.

@svenheden
Copy link
Owner

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.

@JeanMeche
Copy link
Author

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

@lacroixdavid1
Copy link
Contributor

Maybe you can instantiate your MyClass2 this way?

image

@lacroixdavid1
Copy link
Contributor

Note the difference between this one
image

@JeanMeche
Copy link
Author

JeanMeche commented Jun 5, 2019

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 JeanMeche changed the title Shouldn't class objects be converted to optional properties ? Class objects should be converted to optional properties Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants