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

284
Visualizações
TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined'

I wanted to add a Date Picker to my form but i got this error Error: TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined'. Type 'null' is not assignable to type 'string | Date | undefined'. Can anyone help me to solve it?

export class Form extends React.Component<FormProps, {
  fname:   string | undefined;
  lname:    string | undefined;
  birthDate: Date | undefined;
}> {

  constructor(props: FormProps) {
    super(props);

    this.state = {
      fname:    undefined,
      lname:     undefined,
      birthDate:  undefined,
    };
  }
function testField(value: string | undefined | Date, set: React.Dispatch<React.SetStateAction<string | undefined>>): boolean {
  if(value === undefined) {
    set('');
    return false;
  }
  if(value === '') {
    return false;
  }
  return true;
}
  let [fname, setfname] = useState<string | undefined>(undefined);
  let [lsname, setlname] = useState<string | undefined>(undefined);
  let [birthDate, setBirthDate] = useState<Date | null>(null);

const testNext = () => {
    let success = true;
    success = testField(fname, setFname) && success;
    success = testField(lname, setlname) && success;
    success = testField(birthDate, setBirthDate) && success;
   
return (<Fragment>
    <div className="content__form">
      <div className="formcolcontainer">
        <div className="formcol">
          <Input onChange={setFname} name="Fname" error={Fname=== ''}/>
          <Input onChange={setlname} name="lname" error={lname=== ''}/>
        </div>
        <div className="formcol">
            <DatePicker
                id='BirthDate'
                selected={birthDate}
                onChange={(date) => setBirthDate(date)}
                placeholderText="Birth Date"
                />
          </div>
       </div>
     </div>
</Fragment>);}
      
       
          
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It says that, you described your date state variable as can be null or date. However, it can also be undefined type. You can update your date state to this useState<Date|string|undefined>.

Alternatively, you can hover over to onChange props of DatePicker to see the type of "date" param has. Then you update your state type accordingly.

  const [birthDate, setBirthDate] = useState<Date | null | undefined>(null);
about 4 years ago · Juan Pablo Isaza Relatório

0

DatePicker doesn't want null in the 'selected' prop and your birthDate useState is null, you can leave the default value for birthDate empty. And you should use const for React Hooks.

So:

const [birthDate, setBirthDate] = useState<Date>();

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