From 2ecdfd11297cbb35941760a52c1201424e51af6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gromit=20=28=EC=A0=84=EB=AF=BC=EC=9E=AC=29?= <64779472+ssi02014@users.noreply.github.com> Date: Sat, 11 Jan 2025 00:16:27 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20DeepEqual=20=EC=9C=A0=ED=8B=B8=20?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EC=B6=94=EA=B0=80=20(#667)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/few-badgers-dance.md | 5 ++++ .../types/src/DeepPartial/DeepEqual.spec.ts | 26 +++++++++++++++++ packages/types/src/DeepPartial/index.ts | 28 +++++++++++++++++++ packages/types/src/index.ts | 1 + 4 files changed, 60 insertions(+) create mode 100644 .changeset/few-badgers-dance.md create mode 100644 packages/types/src/DeepPartial/DeepEqual.spec.ts create mode 100644 packages/types/src/DeepPartial/index.ts diff --git a/.changeset/few-badgers-dance.md b/.changeset/few-badgers-dance.md new file mode 100644 index 000000000..7b62e1359 --- /dev/null +++ b/.changeset/few-badgers-dance.md @@ -0,0 +1,5 @@ +--- +'@modern-kit/types': minor +--- + +feat(types): DeeoEqual 유틸 타입 추가 - @ssi02014 diff --git a/packages/types/src/DeepPartial/DeepEqual.spec.ts b/packages/types/src/DeepPartial/DeepEqual.spec.ts new file mode 100644 index 000000000..c546bff4e --- /dev/null +++ b/packages/types/src/DeepPartial/DeepEqual.spec.ts @@ -0,0 +1,26 @@ +import { describe, it, expectTypeOf } from 'vitest'; +import { DeepPartial } from '.'; + +describe('DeepPartial', () => { + interface User { + name: string; + age: string; + address: { + street: string; + city: string; + }; + } + + it('DeepPartial 타입은 중첩된 객체의 모든 속성을 선택적으로 만들어줍니다.', () => { + const user: DeepPartial = { + name: 'John', + age: '20', + }; + + expectTypeOf(user).toEqualTypeOf<{ + name?: string; + age?: string; + address?: { street?: string; city?: string }; + }>(); + }); +}); diff --git a/packages/types/src/DeepPartial/index.ts b/packages/types/src/DeepPartial/index.ts new file mode 100644 index 000000000..aff8965e7 --- /dev/null +++ b/packages/types/src/DeepPartial/index.ts @@ -0,0 +1,28 @@ +/** + * @description 객체의 모든 속성을 선택적(optional)으로 만드는 타입 + * 중첩된 객체의 경우에도 재귀적으로 모든 속성을 선택적으로 변환합니다. + * + * @template {Record} T - 변환할 객체 타입 + * @example + * interface User { + * name: string; + * age: number; + * address: { + * street: string; + * city: string; + * }; + * } + * + * type PartialUser = DeepPartial; + * // { + * // name?: string; + * // age?: number; + * // address?: { + * // street?: string; + * // city?: string; + * // }; + * // } + */ +export type DeepPartial> = { + [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; +}; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 201cb63bb..d55524382 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -1,4 +1,5 @@ export * from './Arrayable'; +export * from './DeepPartial'; export * from './DistributiveOmit'; export * from './ExcludeNullish'; export * from './ExtractMapType';