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';