I
I
Ivan2020-07-22 16:24:07
typescript
Ivan, 2020-07-22 16:24:07

What type to specify for children.current in TS?

const handleClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, children: ReactNode): void => {
        if (children) {
            const {current}: HTMLUListElement = children;
            if (current) {
                current.scrollTop = list.current.scrollHeight;
            }
        }
    }
setTimeout(() => handleClick(e, props.children), 0);

throws an error on current :
TS2322: Type 'string | number | true | {} | ReactElement ReactElement Component)> | null) | (new (props: any) => Component<...>)> | ReactNodeArray | ReactPortal' is not assignable to type 'HTMLUListElement'. Type 'string' is not assignable to type 'HTMLUListElement'.
TS2339: Property 'current' does not exist on type 'HTMLUListElement'.


I don't understand how to set current value of HTMLUListElement

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rerurk, 2020-07-22
@Rerurk

Does the compiler still give an error with //@ts-ignore?

A
Aetae, 2020-07-23
@Aetae

Possibly ak:

const {current}: {current: HTMLUListElement} = children;

or like this:
const current = children.current as HTMLUListElement;

You set the type not of current, but of the entire object.
PS Forget about ts-ignore.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question