Property 'children' does not exist on type 'Node'.ts(2339)

x·2022년 5월 31일
0

puppeteer로 동적인 페이지의 컨텐츠를 가져와서 cheerio로 문자를 추출하려고 했다.
typescript에서 rows[0].children[0]의 type은 Node인데 여기엔 children 속성이 없다고 에러를 발생시켰다.

테스트 코드로 디버깅해보면 결과가 잘 나오지만 type을 제대로 지정해주지 않아 문제가 생긴 것이다.

chr(rows[0].children[0].children[0]).text()

위 코드에서 두번쩨 children에서 빨간 줄이 떴다.

첫번째 children에 커서를 갖다 대보니 NodeWithChildren이라는 타입이었다

그래서 아래처럼 as로 타입을 지정해줬다.

const tokenNameDiv = row.children[0] as NodeWithChildren;
const tokenName = chr(tokenNameDiv.children[0]).text();

0개의 댓글