0.1.3 • Published 2 years ago

momo-util-types v0.1.3

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

momo-util-types

npm npm downloads install size npm bundle size

⭐️ 모두모여라의 유틸 타입 라이브러리입니다 ⭐️

Quick Start

$ npm install -D momo-util-types

// or yarn
$ yarn add -D momo-util-types

API

PartialRequired<Type, Keys>

Keys 에서 Required로 변경할 속성 집합을 선택하여 타입을 구성합니다.

type PartialObject = Partial<{ a: 1; b: 2; c: 3 }>;
// { a?: 1, b?: 2, c?: 3 }

type RequiredObject = PartialRequired<PartialObject, "a" | "b" | "c">;
// result : { a: 1, b: 2, c: 3 }

PartialOptional<Type, Keys>

Keys 에서 Optional로 변경할 속성 집합을 선택하여 타입을 구성합니다.

type RequiredObject = { a: 1; b: 2; c: 3 };

type OptinalObject = PartialOptional<RequiredObject, "a" | "b">;
// result : { a?: 1, b?: 2, c: 3 }

Override<Type, Keys>

Keys 에서 속성 집합을 선택하여 기존 타입에 오버라이딩한 새 타입을 구성합니다.

type OriginalObject = { a: 1; b: 2 };
type TargetObject = { a: 3 };

type OverridedObject = Override<OriginalObject, TargetObject>;
// result : { a: 3, b: 2 }

Equal<Type1, Type2>

두 타입의 구성을 비교합니다.

같으면 true, 다르면 false를 타입을 반환합니다.

type TypeA = { a: 1 };
type TypeB = { a: 1 };
type TypeC = { a: 2 };

type MayBeTrue = Equal<TypeA, TypeB>;
// result : true

type MayBeFalse = Equal<TypeA, TypeC>;
// result : false

If<ConditionType, ReturnTypeA, ReturnTypeB>

ConditionType에 따라 다른 타입을 반환합니다.

true일 경우 앞의 타입을, false일 경우 뒤의 타입을 반환합니다.

ConditionTypetrue | false 이어야 합니다.

type LiteralType = If<true, "LiteralTypeA", "LiteralTypeB">;
// result : "LiteralTypeA"

Unionize

객체 타입의 각 요소를 Union 으로 결합시킨 타입을 구성합니다.

type TargetObject = { a: 1; b: 2; c: 3 };

type UnionizedType = Unionize<TargetObject>;
// result : { a: 1 } | { b: 2 } | { c: 3 };

ObjectKey>

객체 타입의 KeyUnion 으로 결합시킨 타입을 구성합니다.

type TargetObject = { a: 1; b: 2; c: 3 };

type KeyType = ObjectKey<TargetObject>;
// result : "a" | "b" | "c";

ObjectValue

객체 타입의 ValueUnion 으로 결합시킨 타입을 구성합니다.

type TargetObject = { a: 1; b: 2; c: 3 };

type ValueType = ObjectKey<TargetObject>;
// result : 1 | 2 | 3

ObjectType<T, Keys?>

객체의 KeyValue의 타입을 지정한 새로운 객체 타입을 구성합니다.

Key 타입의 기본값은 string이며, string | number | symbol 의 하위 타입만 입력할 수 있습니다.

type NewObjectTypeWithStringValue = ObjectKey<string>;
// result : Record<string, string>

type NewObjectTypeWithNumberKey = ObjectKey<string, number>;
// result : Record<number, string>

ArrayElement

대상 배열 타입의 요소들로 구성된 새로운 타입을 구성합니다.

const array = ["a", 1, Symbol("b")];
type NewObjectTypeWithStringValue = ArrayElement<typeof array>;
// result : string | number | symbol