From fec02117d67eb036ff722e785577177bfe10bb66 Mon Sep 17 00:00:00 2001 From: Marcin Gajda Date: Thu, 4 Apr 2024 00:33:10 +0200 Subject: [PATCH] Fix `change-case/keys` types (#337) --- packages/change-case/src/keys.spec.ts | 10 +++++++++- packages/change-case/src/keys.ts | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/change-case/src/keys.spec.ts b/packages/change-case/src/keys.spec.ts index c26a0b28..c6ce1ee2 100644 --- a/packages/change-case/src/keys.spec.ts +++ b/packages/change-case/src/keys.spec.ts @@ -1,7 +1,15 @@ import { describe, it, expect } from "vitest"; import { camelCase } from "./keys"; +import * as changeCase from "./index.js"; -const TEST_CASES: [unknown, number | undefined, unknown][] = [ +type TestCase = [ + unknown, + number | undefined, + unknown, + (changeCase.Options | changeCase.PascalCaseOptions)?, +]; + +const TEST_CASES: TestCase[] = [ [ { first_name: "bob", diff --git a/packages/change-case/src/keys.ts b/packages/change-case/src/keys.ts index 8414348b..f83ec699 100644 --- a/packages/change-case/src/keys.ts +++ b/packages/change-case/src/keys.ts @@ -3,13 +3,15 @@ import * as changeCase from "./index.js"; const isObject = (object: unknown) => object !== null && typeof object === "object"; -function changeKeysFactory( +function changeKeysFactory< + Options extends changeCase.Options = changeCase.Options, +>( changeCase: (input: string, options?: changeCase.Options) => string, -): (object: unknown, depth?: number) => unknown { +): (object: unknown, depth?: number, options?: Options) => unknown { return function changeKeys( object: unknown, depth = 1, - options?: changeCase.Options, + options?: Options, ): unknown { if (depth === 0 || !isObject(object)) return object; @@ -32,14 +34,18 @@ function changeKeysFactory( }; } -export const camelCase = changeKeysFactory(changeCase.camelCase); +export const camelCase = changeKeysFactory( + changeCase.camelCase, +); export const capitalCase = changeKeysFactory(changeCase.capitalCase); export const constantCase = changeKeysFactory(changeCase.constantCase); export const dotCase = changeKeysFactory(changeCase.dotCase); export const trainCase = changeKeysFactory(changeCase.trainCase); export const noCase = changeKeysFactory(changeCase.noCase); export const kebabCase = changeKeysFactory(changeCase.kebabCase); -export const pascalCase = changeKeysFactory(changeCase.pascalCase); +export const pascalCase = changeKeysFactory( + changeCase.pascalCase, +); export const pathCase = changeKeysFactory(changeCase.pathCase); export const sentenceCase = changeKeysFactory(changeCase.sentenceCase); export const snakeCase = changeKeysFactory(changeCase.snakeCase);