TypeScript为什么警告Property 'attr' does not exist on type 'CheerioElement'
?
async function inlineImages(serializedSvg: string) {
const $ = cheerio.load(serializedSvg);
await Promise.all(
Array.from($('image[href^="http"]')).map(async (el: CheerioElement) => {
const url = el.attr("href"); // <--- HERE
if (!url) {
return;
}
const data = await requestBase64(url);
if (!data) {
return;
}
el.attr("href", data); // <--- AND HERE
})
);
return serializedSvg;
}
它发出此警告是因为"CheerioElement"的类型方式没有"attr"属性。
interface CheerioElement {
// Document References
// Node Console
tagName: string;
type: string;
name: string;
attribs: { [attr: string]: string };
children: CheerioElement[];
childNodes: CheerioElement[];
lastChild: CheerioElement;
firstChild: CheerioElement;
next: CheerioElement;
nextSibling: CheerioElement;
prev: CheerioElement;
previousSibling: CheerioElement;
parent: CheerioElement;
parentNode: CheerioElement;
nodeValue: string;
data?: string;
startIndex?: number;
}
参考以下网址的打字员:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cheerio/index.d.ts
"attr"是"Cheerio"类型的属性,调用"CheerioStatic"$()
会返回