質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

1473閲覧

StyledComponentでカスタマイズしたdivタグコンポーネントに、checked属性に値を渡すとTypeScriptでエラーになってしまう

sabx

総合スコア200

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/01/20 06:31

聞きたいこと

styled-compoenntを使用して自前のデザインを適用したチェックボックスを作成しています。
自前のチェックボックスのデザインを整えるために、下のソースコードのようにdivタグでアイコンをラップしているのですが、divタグに存在していない属性であるchecked属性をdivタグにわたすと、TypeScriptでエラーになってしまいます。

  • エラー
TS2769: No overload matches this call.   Overload 1 of 2, '(props: Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | ... 253 more ... | "css"> & { ...; }, "slot" | ... 255 more ... | "css"> & Partial<...>, "slot" | ... 255 more ... | "css"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.     Type '{ children: Element; checked: any; }' is not assignable to type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 254 more ... | "css"> & { ...; }, "slot" | ... 255 more ... | "css"> & Partial<...>, "slot" | ... 255 more ... | "css"> & { ...; } & { ...; }'.       Property 'checked' does not exist on type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 254 more ... | "css"> & { ...; }, "slot" | ... 255 more ... | "css"> & Partial<...>, "slot" | ... 255 more ... | "css"> & { ...; } & { ...; }'.   Overload 2 of 2, '(props: StyledComponentPropsWithAs<"div", any, {}, never, "div">): ReactElement<StyledComponentPropsWithAs<"div", any, {}, never, "div">, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error.     Type '{ children: Element; checked: any; }' is not assignable to type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 254 more ... | "css"> & { ...; }, "slot" | ... 255 more ... | "css"> & Partial<...>, "slot" | ... 255 more ... | "css"> & { ...; } & { ...; }'.       Property 'checked' does not exist on type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 254 more ... | "css"> & { ...; }, "slot" | ... 255 more ... | "css"> & Partial<...>, "slot" | ... 255 more ... | "css"> & { ...; } & { ...; }'.
  • 自前のチェックボックスのコード
import * as React from "react"; import styled from "styled-components"; const Icon = styled.svg` fill: none; stroke: white; stroke-width: 2px; `; const Wrapper = styled.div` ${Icon} { visibility: ${(props) => (props.checked ? "visible" : "hidden")}; } `; export const Checkbox = (props) => { return ( <Wrapper checked={props.checked}> <Icon /> </Wrapper> ); };
  • チェックボックス呼び出し元
import * as React from "react"; import "./styles.css"; import { Checkbox } from "./Checkbox"; export default function App() { const [checked, setChecked] = React.useState(false); return ( <div className="App"> <h1>Hello CodeSandbox</h1> <Checkbox checked={checked} /> </div> ); }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

次のように、追加のpropsを定義することができます。

const Wrapper = styled.div<{ checked: boolean }>` ${Icon} { visibility: ${(props) => (props.checked ? "visible" : "hidden")}; } `;

投稿2021/01/20 07:32

uraway_

総合スコア116

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問