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

ModalContainer.vue で条件分岐をオブジェクトで書き直す #4189

Open
cp-20 opened this issue Dec 7, 2023 · 0 comments
Open
Labels

Comments

@cp-20
Copy link

cp-20 commented Dec 7, 2023

該当箇所 → https://github.com/traPtitech/traQ_S-UI/blob/master/src/components/Modal/ModalContainer.vue#L70-L98

現時点の書き方の問題

  • 条件分岐の書き方が冗長
  • 型のexhaustive check (ちゃんとすべての条件分岐を網羅しているかのチェック) が成されていない
    • 新しい分岐を追加したときに型チェックでエラーを吐かずに、気づかずに修正されない恐れがある

新しい書き方

オブジェクトを使って分岐する

例えばcomponentType = 'a' | 'b' | 'c'という型のtypeに対してcomponentAcomponentBcomponentCをそれぞれ返したいときは、次のように書くことで型安全に書くことができる。

const componentMap: Record<componentType, ComponentPublicInstance> = {
  a: componentA,
  b: componentB,
  c: componentC,
};

return componentMap[type];

この例において、componentType = 'a' | 'b' | 'c' | 'd'となったときはプロパティdに対応するコンポーネントが存在しないため型チェックでエラーを吐いて無事修正を見落とすことがなくなる。

@cp-20 cp-20 added good first issue Good for newcomers refactor labels Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant