github cheeriojs/cheerio v1.0.0-rc.9

latest releases: v1.0.0, v1.0.0-rc.12, v1.0.0-rc.11...
3 years ago

Port to TypeScript

Cheerio has been ported entirely to TypeScript (in #1816)! This eliminates a lot of edge-cases within Cheerio and will allow you to use Cheerio with confidence. This release also features a new documentation website based on TypeDoc, allowing you to quickly navigate all available methods: https://cheerio.js.org


Breaking change: If you were using the function exported by Cheerio directly instead of first load()ing a document, you will now have to update the require to use the default export.

- const cheerio = require("cheerio");
+ const cheerio = require("cheerio").default;

cheerio('div', dom)

Please note that this way of using Cheerio is deprecated and might be removed in a future version. Please consider updating your code to:

const cheerio = require("cheerio");

const $ = cheerio.load(dom)
$('div')

Note: Cheerio uses template literal types to determine return types. These are available starting with TypeScript 4.1, so you might have to bump your TypeScript version.

For TypeScript types, Cheerio now implements the ArrayLike<T> interface. That means that Cheerio instances can contain objects of arbitrary types, but not all methods can be called on them.

The TypeScript compiler will figure out what structures you are operating on:

  • When calling a loaded Cheerio instance with an HTML string like $('<div>'), it will product a Cheerio<Node> type.
    • Node is the base class for DOM elements and includes eg. comment and text nodes.
  • When calling Cheerio with a selector like $('.foo'), it will produce a Cheerio<Element>, as only Elements can be part of the result set.
    • Element is the class representing tags.
  • You can still use $('...').map() to map to arbitrary values, and will get a compiler error when trying to call method that are not supported.
    • Eg. $('.foo').map((i, el) => $(el).text()).attr('test') will no longer be possible, as .attr is not allowed to be called on a Cheerio<string>.

This release does not contain other changes to functionality. Feedback is greatly appreciated; if you encounter a problem, please file an issue!

v1.0.0-rc.6...v1.0.0-rc.9

Don't miss a new cheerio release

NewReleases is sending notifications on new releases.