Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2019 15:22
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import { jsonMember, jsonObject, TypedJSON } from 'typedjson';
import { JsonObjectMetadata } from 'typedjson/js/typedjson/metadata';
import { IJsonObjectOptions } from 'typedjson/js/typedjson/json-object';
import { ParameterlessConstructor } from 'typedjson/js/typedjson/types';
import { Serializer } from 'typedjson/js/typedjson/serializer';
const META_FIELD = '__typedJsonJsonObjectMetadataInformation__';
function findRootMetaInfo(proto): JsonObjectMetadata {
const protoProto = Object.getPrototypeOf(proto);
if (!protoProto || !protoProto[META_FIELD]) {
return proto[META_FIELD];
return findRootMetaInfo(protoProto);
export function object(name: string, options?: IJsonObjectOptions<any>): ClassDecorator {
return (target: Function) => {
jsonObject({ name, ...options })(target as ParameterlessConstructor<any>);
// find root type meta info in TypedJSON, knownTypes needed to understand our type is from our hierarchy
// ..........
class RootType {
public _id: string = "";
class ChildType extends RootType {
const serializer = new TypedJSON(RootType);
Copy link

krizka commented Dec 21, 2019

Hope, it will be helpful for someone who will search, I made some util function and new decorator to achieve polymorphic behaviour, which worked quite well for me. it is added knownTypes to root type automatically, searching for root element:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment