Skip to content

fix: support readonly arrays #1835

@Woodz

Description

@Woodz

Description

Passing a readonly array into plainToInstance matches the scalar overload, meaning that TS infers the result type to be a scalar, not an array. This is very dangerous as this breaks type safety and causes runtime errors

Minimal code-snippet showcasing the problem

import { IsString, validate } from "class-validator";
import { plainToClass } from "class-transformer";

class User {
  @IsString()
  name: string;
}

const RAW: readonly User[] = [{
  name: 'Bob'
}];

const cx = plainToClass(User, RAW);

Expected behavior

cx should be typed as User[]

Actual behavior

cx is typed as User

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions