pbomb / flow-immutable-models

Generates model classes from Flow types using Immutable.js
42 stars 8 forks source link

Support for IntersectionType #25

Closed Heedster closed 6 years ago

Heedster commented 6 years ago

Any plans or thoughts on supporting IntersectionType ? I run in to use cases always where my swagger api models are often inherited from something else. But as of now, since the lib only supports ObjectType, I have to repeat all the properties in each of the type again in the inherited model type.

It would be great if we can support something like

type B = A & {
   bar: string
}

where A is an existing ModelType, so B can be a class that extends A instead of ImmutableModel.

pbomb commented 6 years ago

Hi @Heedster. I don't believe this library can support generating classes from types defined in another file (type "A" in your example). As this library builds a model, it only reads the current file, gathering type information from the AST in that file. It's not like Flow where it statically analyzes the typing across a project/files.

For compatibility with this library, it's recommended to compose your models together instead of inheriting. For example, you could generate a model in a.js and then have an a property in model B that is of type A.

Heedster commented 6 years ago

Hi @Heedster. I don't believe this library can support generating classes from types defined in another file (type "A" in your example). As this library builds a model, it only reads the current file, gathering type information from the AST in that file. It's not like Flow where it statically analyzes the typing across a project/files.

You don't need to. It can be the dev's responsibility in ensuring that a file a.js has a model A that inherits from ImmutableModel or one of its descendants. That way, codegen can assume its there and extend from it, and no such module exists, build would fail and dev would know about it. So there is no issues of it silently causing a bug.

For compatibility with this library, it's recommended to compose your models together instead of inheriting. For example, you could generate a model in a.js and then have an a property in model B that is of type A.

That would depend on the usage of it. In my case, I use this lib to generate model files for my backend apis, and they mostly have models inheriting out of each other (swagger supports this pattern). Inheritance makes sense over composition in many places.

pbomb commented 6 years ago

This library works by generating an ImmutableModel from a simple object type. What you'd like to do is just not how this library is currently designed to work. I agree that inheritance can be useful, but this library is just not well-positioned to support this different case.

I'll think about it though. Not only would the generated code need to be different (extends A vs extends ImmutableModel), but I'm not even sure the current design supports inheritance in how the models are constructed using the fromJS static method.

Heedster commented 6 years ago

I have a small codegen on top of this which takes the classes generated by this on and overrides it in a separate directory. I do this mainly so that i can add more methods (kind of like util methods) into the class manually and my codegen gets it and does not modify those methods. Here I do override the fromJS method without any issues.