Свойство 'attr' не существует для типа 'CheerioElement' - PullRequest
0 голосов
/ 07 апреля 2020

Почему 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;
}

1 Ответ

2 голосов
/ 07 апреля 2020

Он выдает это предупреждение, поскольку способ ввода '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', которое возвращает вызов 'CheerioStati c' $()

...