λ°μν
Intro
μλ νμΈμ. TypeScript νμ μ€ν¬λ¦½νΈμμ refλ₯Ό μ¬μ©νμ¬ scrollTop μμΉλ₯Ό λ³κ²½νλ λμ€ μλμ κ°μ μλ¬κ° λ°μνμμ΅λλ€
someRef?.current?.scrollTop = 0;
TS2779: The left-hand side of an assignment expression may not be an optional property access
Why?
μ€ν μμμ μ΅μ λ 체μ΄λκ³Ό ν λΉμ κ°μ΄ μ¬μ©ν μ μλ€κ³ ν©λλ€.
The following is not supported, although it has some use cases; see Issue #18 for discussion:
optional property assignment: a?.b = c
How to solve the problem
μ½λλ₯Ό λ€μκ³Ό κ°μ΄ μμ ν κ²°κ³Ό, λ μ΄μ ν΄λΉ μλ¬κ° λ°μνμ§ μμμ΅λλ€.
if (someRef?.current?.scrollTop) {
someRef.current.scrollTop = 0;
}
μ΄ μΈμλ μλμ κ°μ΄ μμ νμ€ μλ μμ΅λλ€.
if (someRef.current.scrollTop != null) {
someRef.current.scrollTop = 0;
}
https://github.com/tc39/proposal-optional-chaining#not-supported
λ°μν