Skip to content

camelize does not properly match target interface for optional arrays fields #69

Description

@wolak041

Description

When using the camelize function, the output does not strictly conform to the target interface, when converting an array of objects (RawUser[]) to a camelized format (User[]), the resulting type does not guarantee compliance with the User interface. This leads to a TypeScript error when attempting to assign the camelized result to a variable of type User[]. However, if the foreign_languages property is not optional or is not an array, the error does not occur.

Example Code

import camelize from 'camelize-ts';

interface RawUser {
    id: number;
    last_name: string;
    foreign_languages?: string[];
}
interface User {
    id: number;
    lastName: string;
    foreignLanguages?: string[];
}
const rawUsers = [] as RawUser[];

// TypeScript Error:
// Type 'CamelizeObject<RawUser, false>[]' is not assignable to type 'User[]'.
//   Type 'CamelizeObject<RawUser, false>' is not assignable to type 'User'.
//     Types of property 'foreignLanguages' are incompatible.
//       Type 'CamelizeObject<string[] | undefined, false>' is not assignable to type 'string[] | undefined'.
//         Type 'CamelizeObject<string[], false>' is missing the following properties from type 'string[]': [Symbol.iterator], [Symbol.unscopables]ts(2322)
const camelizedUser: User[] = camelize(rawUsers);

Expected Behavior

Optional properties are handled properly and does not trigger TypeScript error - the camelizedUser variable should strictly match the User[] interface

Actual Behavior

The camelize function produces a type CamelizeObject<RawUser, false>[], which is not assignable to User[]. The specific issue arises with the foreignLanguages property, where the type CamelizeObject<string[] | undefined, false> is not compatible with string[] | undefined.

Error Message

Type 'CamelizeObject<RawUser, false>[]' is not assignable to type 'User[]'.
  Type 'CamelizeObject<RawUser, false>' is not assignable to type 'User'.
    Types of property 'foreignLanguages' are incompatible.
      Type 'CamelizeObject<string[] | undefined, false>' is not assignable to type 'string[] | undefined'.
        Type 'CamelizeObject<string[], false>' is missing the following properties from type 'string[]': [Symbol.iterator], [Symbol.unscopables]ts(2322)

Environment

  • camelize-ts version: 3.0.0
  • TypeScript version: 5.7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions