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

feature: Support type-safe object literals in plainToClass #503

Closed
imgx64 opened this issue Nov 5, 2020 · 4 comments · May be fixed by #1451
Closed

feature: Support type-safe object literals in plainToClass #503

imgx64 opened this issue Nov 5, 2020 · 4 comments · May be fixed by #1451
Labels
status: wontfix type: feature Issues related to new features.

Comments

@imgx64
Copy link

imgx64 commented Nov 5, 2020

Description

When creating a complex object, it's sometimes desirable to use plainToClass with an inline object literal. Unfortunately, the plain object will not be type checked:

class User {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

// no error for missing age, bad
const user1 = plainToClass(User, {
  name: 'test',
});

One way to do type checking is to provide the type explicitly. But you have to type the class name 3 times to get this to work:

// error, good but verbose
const user2 = plainToClass<User, User>(User, {
  name: 'test',
});

Proposed solution

In my projects, I added a helper function:

function plainToClassInline<T>(cls: ClassType<T>, plain: T, options?: ClassTransformOptions): T;
function plainToClassInline<T>(cls: ClassType<T>, plain: T[], options?: ClassTransformOptions): T[];
function plainToClassInline<T>(cls: ClassType<T>, plain: T | T[], options?: ClassTransformOptions): T | T[] {
  return plainToClass(cls, plain, options);
}

(I'm not sure about the name. It could be made clearer)

Now you get proper type checking without repeating the class name:

// error. good and concise
const user3 = plainToClassInline(User, {
  name: 'test',
});
@lazarljubenovic
Copy link

This would work only for trivial cases, with no renamed or excluded properties.

@NoNameProvided NoNameProvided added status: wontfix type: feature Issues related to new features. labels Feb 15, 2021
@NoNameProvided
Copy link
Member

As @lazarljubenovic mentions, this only works until you are not renaming properties, in that case the two type would not match and you wouldn't be able to call plainToClass. What would be cool to infer the classToPlain return type but I am not aware of any way to do that with Typescript.

@NoNameProvided
Copy link
Member

I will close this as this is not possible I believe, but I will keep my eyes open for any such thing in the future as I believe automatic type inferring is a very useful feature.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: wontfix type: feature Issues related to new features.
Development

Successfully merging a pull request may close this issue.

3 participants