Closed theodorebugnet closed 2 years ago
Are you using the latest version of the codegen package? This issue has already been fixed.
There should be special function in marshal.ts
to deal with enums.
export function enumFromJson<E extends object>(json: unknown, enumObject: E): E[keyof E] {
assert(typeof json == 'string', 'invalid enum value')
let val = (enumObject as any)[json]
assert(typeof val == 'string', `invalid enum value`)
return val as any
}
Consider a schema such as:
codegen
will then generate asrc/model/generated/_myObject.ts
file, containing a constructor that allows deserializing from JSON using marshal. However, it treats the enum field as a string, causing the typescript build to fail (with the errorType 'string' is not assignable to type 'MyEnum'.
):I believe the fix would be to generate a type cast for the enum type, e.g.