sindresorhus / camelcase-keys

Convert object keys to camel case
MIT License
679 stars 92 forks source link

[TypeScript] If the `deep: true` option is set, even the object's properties will be converted. #111

Closed yutak23 closed 1 year ago

yutak23 commented 1 year ago

I use this library in TypeScript (thank you for developing it).

When the following implementation, the type definitions of Date and class fields and methods are also converted to camelCase.

class Point {
    x: number;
    y: number;
    addPoint(point: Point): Point {
        return ...;
    }
    constructor(x: number, y: number) {
        ...
    }
}

const point = new Point(0, 10);

const result = camelcaseKeys({ foo_Baz: point, foo_bar: new Date() }, { deep: true });
// type structure of `result`
const result: {
    fooBaz: {
        x: number;
        y: number;
        addPoint: (point: Point) => Point;
    };
    fooBar: {
        toString: () => string;
        toDateString: () => string;
        toTimeString: () => string;
        toLocaleString: {
            (): string;
            (locales?: string | string[] | undefined, options?: Intl.DateTimeFormatOptions | undefined): string;
            (locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions | undefined): string;
        };
        ... 39 more ...;
        [Symbol.toPrimitive]: {
            ...;
        };
    };
}

I think the expected value is below.

const result: {
    fooBaz: Point;
    fooBar: Date
}