C
C
colorkid2020-02-24 11:29:30
typescript
colorkid, 2020-02-24 11:29:30

How to type the following function?

I am mastering TS, writing a small application, here is a function from it in js:

const getLinksOnPage = () => {
  return [...document.querySelectorAll('img')].map(item => item.src);
}

I try to type, I get something like

const getLinksOnPage = (): [string] => {
  return [...document.querySelectorAll('img')].map(item => item.src);
};

Swears at
Property '0' is missing in type 'any[]' but required in type '[string]'

and on
Type 'NodeListOf<HTMLImageElement>' is not an array type


Googled, but no luck.

How to type such a function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2020-02-24
@colorkid

[string] is a tuple that contains 1 string
string[] will be an array of strings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question