Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
TS2322: Type 'Atrribute' is not assignable to type 'boolean | undefined'. Toggle

I tried first isToggle and onToggle to be a boolean type, but it still warning about the types after the change you can see below.

This is error of checked: Type 'Atrribute' is not assignable to type 'boolean | undefined'.

This is error of onChange : Type 'Atrribute' is not assignable to type 'ChangeEventHandler'. Type 'Atrribute' provides no match for the signature '(event: ChangeEvent): void'.

import React, {ChangeEventHandler} from "react";
import classNames from "classnames";
import './SwitchToggle.css';

interface Atrribute{
    rounded:boolean;
    isToggled:boolean|undefined;
    onToggle:ChangeEventHandler<HTMLInputElement>;
}

const SwitchToggle =(rounded:Atrribute ,isToggled:Atrribute,onToggle:Atrribute)=>{

    const sliderCX = classNames("slider",{
        "rounded":rounded
    })

    return(
        <label className={"switch"}>
            <input type={"checkbox"} checked={isToggled} onChange={onToggle}/>
            <span className={sliderCX}/>
        </label>
    )
}

export default SwitchToggle;

I also use this code in main and its work fine

const [isToggled,setTisToggled] = useState(false);

<SwitchToggle isToggled{...SwitchToggle.prototype.isToggled} onToggle={()=>setTisToggled(!SwitchToggle.prototype.isToggle)}/>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This isn't how you destructure function arguments:

const SwitchToggle = (rounded:Atrribute ,isToggled:Atrribute,onToggle:Atrribute) => {...}

What you're telling TypeScript here is that this function will receive three arguments, each of them being of type Atrribute. But instead this function will receive one argument, of type Atrribute, from which you want to destructure three properties.

To destructure an object, use the {} destructuring syntax:

const SwitchToggle = ({ rounded, isToggled, onToggle } : Atrribute) => {...}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think you should write like this...

const SwitchToggle =({ rounded, isToggled ,onToggle }:Atrribute)=>{
about 4 years ago · Juan Pablo Isaza Relatório

0

Your props declaration is wrong. You can't write your props this way :

const SwitchToggle =(rounded:Atrribute ,isToggled:Atrribute,onToggle:Atrribute) => {...}

Atrribute is the type of your entire props object, so you should write it this way :

const SwitchToggle =(props:Atrribute) => {...}

Or, if you want to destructure your object, then it'll be something like :

const SwitchToggle =({rounded, isToggled, onToggle}:Atrribute) => {...}

P.S. : You misswrote Atrribute.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda