Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda