I have this classes:
class Book {
constructor({
name = ''
} = {}) {
this.name = name
}
get test() {
return 8 * 8
}
}
class User {
contructor({
name = '',
email = '',
books = []
} = {}) {
this.name = name
this.email = email
this.books = plainToClass(Book, books)
}
get custom() {
return this.name + this.email
}
}
When I do:
let users = plainToClass(User, [{name: 'John', email: '[email protected]', books: [{name: 'Maths'}]}]);
users are transformed as expected but the books don't have test property.
What is the right way to do this?
class Book {
name: string;
constructor(args: Book) {
Object.assign(this, args);
}
get test() {
return 8 * 8
}
}
import { Type } from 'class-transformer';
import { Book } from './book';
class User {
name: string;
email: string
@Type(() => Book)
books: Book[];
constructor(args: User) {
Object.assign(this, args);
}
get custom() {
return this.name + this.email
}
}
Not sure if this is best practice but this is how I've done it.
@lucasklaassen answer is correct, you need to use the @Type decorator to define property type as user made classes.
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.