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

TypeScriptのAny不許可によって冗長になったコードをリファクタリングしたい #26

Open
0si43 opened this issue Oct 19, 2021 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@0si43
Copy link
Owner

0si43 commented Oct 19, 2021

上手くダックタイピングみたいなことができるといいんだけど……
個人的に気になってるのが下記二箇所。
(どちらもrenderNotionBlock.tsx)

type richText = {
  type: 'text'
  text: {
    content: string
    link: {
      url: string
    } | null
  }
  annotations: {
    bold: boolean
    italic: boolean
    strikethrough: boolean
    underline: boolean
    code: boolean
    color:
      | 'default'
      | 'gray'
      | 'brown'
      | 'orange'
      | 'yellow'
      | 'green'
      | 'blue'
      | 'purple'
      | 'pink'
      | 'red'
      | 'gray_background'
      | 'brown_background'
      | 'orange_background'
      | 'yellow_background'
      | 'green_background'
      | 'blue_background'
      | 'purple_background'
      | 'pink_background'
      | 'red_background'
  }
  plain_text: string
  href: string | null
}

↑Notionの型定義ファイルから丸コピペしている。
さすがにもっといい手があるはず

export const renderBlock = (block: blockWithChildren) => {
  const { type, id } = block

  switch (type) {
    case 'paragraph':
      return (
        <p>
          <TextComponent richTexts={block.paragraph.text as richText[]} />
        </p>
      )
    case 'heading_1':
// ……

↑ほとんどのケースがparagraphと同じくtext返すだけなのに、リテラル型が上手く扱えずにこうなった

@0si43 0si43 added the help wanted Extra attention is needed label Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant