Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!