Skip to content

Are TypeScript types wrong or are we all doing something wrong? #1638

@ericmorand

Description

@ericmorand

I've been using parse5 for months with TypeScript and I always had to fight with the typings, assuming I was doing something wrong but unable to understand what. Today I took the time to test the code samples that are shipped with the typings, for example:

And the code sample itself doesn't satisfy the typechecker:

import * as parse5 from 'parse5';

const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');

// Serializes a document.
const html = parse5.serialize(document);

// Serializes the <html> element content.
const str = parse5.serialize(document.childNodes[1]); // TS2345: Argument of type ChildNode is not assignable to parameter of type ParentNode

console.log(str); //> '<head></head><body>Hi there!</body>'

I feel reassured - I may not be doing anything wrong - and concerned at the same time: parse5 is written in TypeScript. I'm not sure I understand how typings could be wrong if the code can be tested and built.

Note that the above code works perfectly fine once compiled into JavaScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions