From 482bd844a3ec38b0cd4e240d0c5109c2cb530534 Mon Sep 17 00:00:00 2001 From: yutak23 <79501292+yutak23@users.noreply.github.com> Date: Tue, 17 Oct 2023 17:24:18 +0900 Subject: [PATCH] Fix setting default value for the TypeScript type (#117) --- index.d.ts | 12 ++++++------ index.test-d.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 5d7542a..1db6306 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,7 +11,7 @@ Return a default type if input type is nil. @template T - Input type. @template U - Default type. */ -type WithDefault = T extends undefined | void | null ? U : T; // eslint-disable-line @typescript-eslint/ban-types +type WithDefault = T extends undefined | void | null ? U : T; // eslint-disable-line @typescript-eslint/ban-types // TODO: Replace this with https://github.com/sindresorhus/type-fest/blob/main/source/includes.d.ts /** @@ -237,9 +237,9 @@ export default function camelcaseKeys< options?: OptionsType ): CamelCaseKeys< T, -WithDefault, -WithDefault, -WithDefault, -WithDefault, -WithDefault +WithDefault<'deep' extends keyof OptionsType ? OptionsType['deep'] : undefined, false>, +WithDefault<'pascalCase' extends keyof OptionsType ? OptionsType['pascalCase'] : undefined, false>, +WithDefault<'preserveConsecutiveUppercase' extends keyof OptionsType ? OptionsType['preserveConsecutiveUppercase'] : undefined, false>, +WithDefault<'exclude' extends keyof OptionsType ? OptionsType['exclude'] : undefined, EmptyTuple>, +WithDefault<'stopPaths' extends keyof OptionsType ? OptionsType['stopPaths'] : undefined, EmptyTuple> >; diff --git a/index.test-d.ts b/index.test-d.ts index 989d2c6..467d2b3 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -453,3 +453,21 @@ expectType<[ }), ]), ); + +// Test for function with inferred type +// eslint-disable-next-line @typescript-eslint/comma-dangle +function camelcaseKeysDeep< + T extends Record | readonly unknown[] +>(response: T): CamelCaseKeys { + return camelcaseKeys(response, {deep: true}); +} + +// eslint-disable-next-line @typescript-eslint/comma-dangle +function camelcaseKeysPascalCase< + T extends Record | readonly unknown[] +>(response: T): CamelCaseKeys { + return camelcaseKeys(response, {pascalCase: true}); +} + +expectType<{fooBar: {hogeHoge: string}}>(camelcaseKeysDeep({foo_bar: {hoge_hoge: 'hoge_hoge'}})); +expectType<{FooBar: string}>(camelcaseKeysPascalCase({foo_bar: 'foo_bar'}));