TypedJSON
TypedJSON copied to clipboard
Polymorphic / generic types?
I'm trying to use this monads library to convert my nullable / undefined fields from my API calls, to rust-style Options. With it I can provide compile-time checking for nullable fields.
I get the following error from the example below.
'OptionT' only refer to a type, but is being used as a value here.
import 'reflect-metadata';
import { jsonObject, jsonMember, TypedJSON } from 'typedjson';
import { Some, Option as OptionT } from "@sniptt/monads";
@jsonObject
class MyDataClass {
@jsonMember
prop1?: string;
@jsonMember
prop2: OptionT<string>;
}
let serializer = new TypedJSON(MyDataClass);
serializer.mapType(
OptionT, // Option doesn't work here for some reason, because its generic?
{
deserializer: json => json == null ? json : Some(json),
serializer: value => value == null ? value : value.unwrap(),
});
const obj = new MyDataClass();
obj.prop1 = "opt";
obj.prop2 = Some("22");
const json = serializer.stringify(obj);
const object2 = serializer.parse(json);
console.log(json);
console.log(object2);
object2 instanceof MyDataClass; // true
output
{"prop1":"opt","prop2":{}} // Not serializing / stringifying correctly
MyDataClass { prop1: 'opt', prop2: {} }
https://github.com/sniptt-official/monads/issues/105