Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript parse5 with WTF typings #344

Closed
c0ncentus opened this issue May 13, 2021 · 2 comments 路 Fixed by #362
Closed

Typescript parse5 with WTF typings #344

c0ncentus opened this issue May 13, 2021 · 2 comments 路 Fixed by #362

Comments

@c0ncentus
Copy link

hi,

image

WTF ?
How do you know if the child have no child (html string) ? Do you spying my files ? 馃憥

So I must put "as" everywhere

because parser5 say "if you are a child, you have no child", and "if you are a perent then you have no parent". Is why parse5's typings sucks.

image

Okay AST is complex but it's why you should allow one type and make available Parent and Child types

type Node = ParentNode | ChildNode

@43081j
Copy link
Collaborator

43081j commented Jul 11, 2021

The AST isn't complex. The types were just badly written and poorly maintained.

I rewrote them mostly from the ground up a while ago so they are much better now but still have some complexities due to the model structure here in parse5.

it isn't as simple as Node = ParentNode | ChildNode.

childNodes: Node[] would make you happy but would be incorrect, as a child can't be any node, it can only be those capable of having a parent (a ChildNode).

childNodes: ChildNode[] is correct but makes you unhappy, as a child can only possibly be a ChildNode (i.e. the types of Node which have a parent).

Unfortunately there's no obvious middle ground here. Really its like having unknown[], and up to you to cast as needed.

This ultimately drills down to the fact that the source models are not branded/tagged unions. Types would be much, much nicer if that was the case.

@fb55 fb55 linked a pull request Jan 13, 2022 that will close this issue
4 tasks
@fb55 fb55 closed this as completed in #362 Feb 7, 2022
@Johnrobmiller
Copy link

Johnrobmiller commented Jun 21, 2022

+1 for this post, new users using parse5 have no way of knowing that they need to explicity cast their node parents/children as the post above suggests.

When I first installed parse5 and started trying to use it, I just assumed that types were either broken or required some obscure generic somewhere. It took some digging to find this post.

It's not good practice to explicitly cast the types of the return of a function. This is poor type safety. With this said, it might be good to refactor some of these types to ensure that the return of a function is always strongly typed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants