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(?)
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
});
}