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

Polymorphic / generic types? #196

Open
dessalines opened this issue Jun 9, 2022 · 0 comments
Open

Polymorphic / generic types? #196

dessalines opened this issue Jun 9, 2022 · 0 comments

Comments

@dessalines
Copy link

dessalines commented Jun 9, 2022

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: {} }

thames-technology/monads#105

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

1 participant