Skip to content

feature: Add Plain Typing. #789

Open
@BenMcLean981

Description

@BenMcLean981

Description

I think its a little odd that with Typescript all we know about a plain object is that its a of type Record<string, any>

For example:

import { classToPlain } from "class-transformer;

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

plain = classToPlain(new Point(5, 4));
/* plain is of type Record<string, any>
  * But we in our heads sort of know that plain should be of type
  * {
  *     x: number;
  *     y: number;
  * }
  * /


Proposed solution

Before finding this library I was working on a generic type that would find properties to be carried over to a "plain" functionless object.

type PrimitiveKeys<T> = {
  [key in keyof T]: T[key] extends Function | undefined ? never : key;
}[keyof T];

type Plain<T> = {
  [key in PrimitiveKeys<T>]: T[key];
};

type PlainPoint = Plain<T>; //
{
    x: number;
    y: number;
}

The problems I ran into with this were with nested arrays and objects. I'm not super great with typescript typings so Im not really sure what the best way to fix this is (Which is why I'm offloading the functionality to class-transformer).

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions