Package: typemallow2
typemallow 2An elegant and automatic solution for generating/outputting Typescript interfaces from your Marshmallow SchemasI updated the typemallow package so it could Handle enums in typescript and more recent versions of marshmallow.
Original work is from adenh93.Usage:Using typemallow2 is simple.First, install the packagepip install typemallow2Next, for your Marshmallow schemas that you wish to generate Typescript interfaces for, simply importts_interfaceandgenerate_tsfrom thetypemallowmodule, and prepend the@ts_interface()class decorator to your Marshmallow schema class.All that is required to generate your Typescript interfaces is to call thegenerate_ts()function, and provide a filepath as a parameter to output the result.main.pyfromtypemallow2importts_interface,generate_ts@ts_interface()classFoo(Schema):some_field=fields.Str()another_field=fields.Date()generate_ts('./output.ts')output.tsexportinterfaceFoo{some_field:string;another_field:date;}typemalloweven supports Nested Schema fields and enums.main.pyfromtypemallow2importts_interface,generate_ts,ts_enumfrommarshmallowimportSchema,fieldsfrommarshmallow_enumimportEnumFieldfromenumimportEnum,auto@ts_enum(value_is_auto=True)classMyAutoEnum(Enum):enum1=auto()enum2=auto()@ts_enum(value_is_auto=False)classMyNonAutoEnum(Enum):enum1=100enum2=200enum3="Baguette"@ts_interface()classBoo(Schema):some_field=fields.Str()my_enum_field=EnumField(MyAutoEnum)@ts_interface()classFoo(Schema):some_field=fields.Str()another_field=fields.Str()my_field=fields.Bool()my_interface_field=fields.Nested(Boo,many=False)generate_ts('./output.ts')output.tsexportenumMyAutoEnum{enum1,enum2,}exportinterfaceBar{some_field:string;my_enum_field:MyAutoEnum;}exportinterfaceFoo{some_field:string;another_field:string;my_field:boolean;my_interface_field:Bar;my_interfaces_fields:Bar[];}Extended Usage:The@ts_interface()decorator function accepts an optional parameter,context, which defaults to... well... 'default'."Why is this the case?"When a Marshmallow Schema is identified with with@ts_interfacedecorator, it is added to a list in a dictionary of schemas, with the dictionary key being the value provided to thecontextparameter. If you were to provide different contexts for each schema, additional keys will be created if they do not exist, or the schema will simply be appended to the list at the existing key.This comes in handy, as thegenerate_ts()functionalsoaccepts an optionalcontextparameter, which will filter only schemas in the dictionary at the specific key.This is useful if you wish to output different contexts to different files, e.g.main.py...fromtypemallow2importts_interface,generate_ts@ts_interface(context='internal')classFoo(Schema):foo=fields.Str()@ts_interface(context='internal')classBar(Schema):bar=fields.Str()@ts_interface(context='external')classFooBar(Schema):foo_bar=fields.Str()'''we're telling typemallow2 that we only want to generate interfaces from Schemas withan 'internal' context to './internal.ts''''generate_ts('./internal.ts',context='internal')'''only generate interfaces from Schemas with an 'external' context to './external.ts''''generate_ts('./external.ts',context='external')internal.tsexportinterfaceFoo{foo:string;}exportinterfaceBar{bar:string;}external.tsexportinterfaceFooBar{foo_bar:string;}