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

295
Visualizações
Typescript: how to assign value to Element.scrollIntoView's parameters?

I was trying to make a method to use "scrollIntoView" and optionally set its parameters, but when trying to set the block parameter, it says:

Type 'string' is not assignable to type 'ScrollLogicalPosition | undefined'.

And when trying to set behaviour gives:

Type 'string' is not assignable to type 'ScrollBehavior | undefined'

Here is code:

scrollToFirstElementOfClassName(className: string, blockValue: string = "center",behavior: string = 'smooth') {

    const elms = document.getElementsByClassName(className);

      elms[0].scrollIntoView({
        block: blockValue,
        behavior: behavior
      });
    
  }

So "ScrollLogicalPosition" and "ScrollBehavior" are types? So I ned to convert the string to those types in some way(?)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Typescript does not like to reconcile string literal with string literals as types (i.e Union types)

Your blockValue has the correct value, but Typescript wants its type to be ScrollLogicalPosition, which is a union/subset of string, but not actually string.

I believe a fix is changing the type of your function parameters:

scrollToFirstElementOfClassName(className: string, blockValue: ScrollLogicalPosition = "center", behavior: ScrollBehavior = 'smooth') {

    const elms = document.getElementsByClassName(className);

      elms[0].scrollIntoView({
        block: blockValue,
        behavior: behavior
      });
    
  }
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