From 264cc707136cb854527b0d6a4c9956b1db11a423 Mon Sep 17 00:00:00 2001 From: Masa-Shin <34234442+Masa-Shin@users.noreply.github.com> Date: Thu, 23 Jun 2022 02:59:52 +0900 Subject: [PATCH] Improve type definition (#92) --- index.d.ts | 40 +++++++++++++++++++++++----------------- index.test-d.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/index.d.ts b/index.d.ts index e1e9416..f4ee3c3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -45,13 +45,16 @@ export type CamelCaseKeys< > = T extends readonly any[] // Handle arrays or tuples. ? { - [P in keyof T]: CamelCaseKeys< - T[P], - Deep, - IsPascalCase, - Exclude, - StopPaths - >; + // eslint-disable-next-line @typescript-eslint/ban-types + [P in keyof T]: {} extends CamelCaseKeys + ? T[P] + : CamelCaseKeys< + T[P], + Deep, + IsPascalCase, + Exclude, + StopPaths + >; } : T extends Record // Handle objects. @@ -64,16 +67,19 @@ export type CamelCaseKeys< true, ] ? T[P] - : [Deep] extends [true] - ? CamelCaseKeys< - T[P], - Deep, - IsPascalCase, - Exclude, - StopPaths, - AppendPath - > - : T[P]; + // eslint-disable-next-line @typescript-eslint/ban-types + : {} extends CamelCaseKeys + ? T[P] + : [Deep] extends [true] + ? CamelCaseKeys< + T[P], + Deep, + IsPascalCase, + Exclude, + StopPaths, + AppendPath + > + : T[P]; } // Return anything else as-is. : T; diff --git a/index.test-d.ts b/index.test-d.ts index f2b2f99..6017f4d 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/naming-convention */ +/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/indent */ import {expectType, expectAssignable, expectNotType} from 'tsd'; import camelcaseKeys, {type CamelCaseKeys} from './index.js'; @@ -350,3 +350,31 @@ expectNotType( exclude, }), ); + +expectType<{ + funcFoo: () => 'foo'; + recordBar: {foo: string}; + promiseBaz: Promise; +}>( + camelcaseKeys({ + func_foo: () => 'foo', + record_bar: {foo: 'bar'}, + promise_baz: new Promise(resolve => { + resolve(true); + }), + }), +); + +expectType<[ + () => 'foo', + {foo: string}, + Promise, +]>( + camelcaseKeys([ + () => 'foo', + {foo: 'bar'}, + new Promise(resolve => { + resolve(true); + }), + ]), +);