Skip to content

How to specify an element is a text one? #137

Answered by wooorm
ooker777 asked this question in Q&A
Discussion options

You must be logged in to vote

TypeScript gives you a type error because you have a type error.
If you want types, use TS. TS will give you errors when you make problems. This is that.

How to use TS:

You store things in variables, and perform checks on them:

const tree = fromHtml("<h1>Hi</h1>>", { fragment: true }); // Root
const h1 = select("h1", tree) // Element
if (!h1) throw new Error("main heading missing")
const head = h1.children[0] // ElementContent
if (head.type !== 'text') throw new Error("there are non-text things in the head!")
console.log(head.value)

However, do not do you want you do. Your goal is to get the text of a heading. Headings can contain other things than text (as the types indicate). If you wan…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ooker777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants