You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The | Object in each of the main types is both correct and well intentioned, but it renders the type useless to the type checker, as foo | Object simplifies to Object for most types. (this might not have been true prior to typescript 2.4.1).
I can use parse5.AST.Default.Node and friends in my code, but the moment that I use any method on parse5, even ones on the default tree adapter like parse5.treeAdapters.default.getChildNodes I'm back to having Objects again.
One starting place for an improvement would be to make the TreeAdapter interface generic over its node type.
interfaceTreeAdapter<Node>{/*...*/}
One could go a step farther and make the interface generic over all of the subtypes, getting something like:
This is unwieldy, but users would rarely if ever encounter it unless they were writing their own TreeAdapters, as the default and htmlparser2 adapters would have all of the type variables filled in.
The text was updated successfully, but these errors were encountered:
One could go a step farther and make the interface generic over all of the subtypes
I've initially had thoughts about it, but eventually decided to implement it in the simplified manner. If you are keen to implement this, PR is always welcome!
This is actually solvable even easier. The best way would be to use union types and discriminators for type narrowing, which is already possible with the current design and which I do a lot in my code base because it allows for a lot of flexibility. An example:
exportinterfaceDefaultTreeDocumentType{nodeName: "#documentType"// other fields}exportinterfaceDefaultTreeDocument{nodeName: "#document"// other fields}exportinterfaceDefaultTreeDocumentFragment{nodeName: "#document-fragment"// other fields}exportinterfaceDefaultTreeCommendNode{nodeName: "#comment"// other fields}exportinterfaceDefaultTreeTextNode{nodeName: "#text"// other fields}exportinterfaceDefaultTreeElement{nodeName: "string"// catch all remaining nodes// other fields}exporttypeDefaultNode=|DefaultTreeDocumentType|DefaultTreeDocument|DefaultTreeDocumentFragment|DefaultTreeCommendNode|DefaultTreeTextNode|DefaultTreeElement
Now, if you check for the type, the TypeScript compiler will immediately use the correct type
constnode: DefaultNode= ...
if(node.type==="#text"){// TypeScript now knows that node is of type DefaultTreeTextNode
console.log(node.value)}
The
| Object
in each of the main types is both correct and well intentioned, but it renders the type useless to the type checker, asfoo | Object
simplifies toObject
for most types. (this might not have been true prior to typescript 2.4.1).I can use
parse5.AST.Default.Node
and friends in my code, but the moment that I use any method on parse5, even ones on the default tree adapter likeparse5.treeAdapters.default.getChildNodes
I'm back to havingObject
s again.One starting place for an improvement would be to make the TreeAdapter interface generic over its node type.
One could go a step farther and make the interface generic over all of the subtypes, getting something like:
This is unwieldy, but users would rarely if ever encounter it unless they were writing their own TreeAdapters, as the default and htmlparser2 adapters would have all of the type variables filled in.
The text was updated successfully, but these errors were encountered: