Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: invariant #561

Open
1 of 2 tasks
ssi02014 opened this issue Nov 4, 2024 · 2 comments
Open
1 of 2 tasks

[Feature]: invariant #561

ssi02014 opened this issue Nov 4, 2024 · 2 comments
Assignees
Labels
feature 새로운 기능 추가 @modern-kit/utils @modern-kit/utils

Comments

@ssi02014
Copy link
Contributor

ssi02014 commented Nov 4, 2024

Package Scope

  • 기존 패키지에 기능 추가
  • 새로운 패키지
  • Package name: @modern-kit/utils/common

Overview

주어진 condition이 false면 에러를 던져주는 함수입니다.

interface

function invariant(condition: boolean, message: string): asserts condition

Usage

function example(value: string | null) {
  invariant(value !== null, "Value should not be null");

  // 이후로는 TypeScript가 value를 string으로 안전하게 인식함
  console.log(value.length); // value는 이제 string 타입으로 간주됨
}
@ssi02014
Copy link
Contributor Author

ssi02014 commented Nov 4, 2024

function invariant(condition: boolean, message: string): asserts condition {
  if (!condition) {
    throw new Error(message);
  }
}

@ssi02014 ssi02014 added feature 새로운 기능 추가 @modern-kit/utils @modern-kit/utils labels Nov 4, 2024
@Sangminnn Sangminnn self-assigned this Nov 17, 2024
@ssi02014
Copy link
Contributor Author

ssi02014 commented Dec 4, 2024

export function invariant(value: unknown, message: string): asserts value {
  if (value === false || isNil(value)) {
    throw new Error(message);
  }
}

boolean이 아닌 다양한 값이 들어오게끔 condition을 value로 변경하고, 다양한 타입이 오게끔 unknown으로 지정합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 새로운 기능 추가 @modern-kit/utils @modern-kit/utils
Projects
None yet
Development

No branches or pull requests

2 participants