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
Description
When using the
camelizefunction, 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 theUserinterface. This leads to a TypeScript error when attempting to assign the camelized result to a variable of typeUser[]. However, if theforeign_languagesproperty is not optional or is not an array, the error does not occur.Example Code
Expected Behavior
Optional properties are handled properly and does not trigger TypeScript error - the
camelizedUservariable should strictly match theUser[]interfaceActual Behavior
The
camelizefunction produces a typeCamelizeObject<RawUser, false>[], which is not assignable toUser[]. The specific issue arises with theforeignLanguagesproperty, where the typeCamelizeObject<string[] | undefined, false>is not compatible withstring[] | undefined.Error Message
Environment