C
C
colorkid2020-02-24 17:54:11
typescript
colorkid, 2020-02-24 17:54:11

How to type an application?

I'm learning TS. I am writing an attachment, some questions arise, and here is one of them?

Is it worth typing 100% into an application on react + redux i.e. in general, everything, and components and redax and a folder with utilities?

How "Deep" is it worth optimizing specific functions, for example:

export const getLinksOnPage = (): string[] => {
  const allLinksOnPage : NodeListOf<HTMLImageElement> = document.querySelectorAll('img');
  return Array.from(allLinksOnPage).map(item => item.src);
};

Here I type what the function returns, and what the function can take as arguments (in this example, not, but suppose) - I agree with this cool, useful, offset. But inside the function there is also, let's say, such an intermediate typing ( NodeListOf<HTMLImageElement>) with which I do not quite agree, i.e. I'm not sure if such things should be typed at all, can there be problems here - I'm not sure, BUT there are problems that I want to do some things in code, let's say [...new Set(arr)], to get only unique elements of the array, and ts swears and there are a lot of such moments where I write the code is not exactly how I want, but how ts forces me. Do you think it's worth typing everything at all?

Answer the question

In order to leave comments, you need to log in

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

ts swears and there are a lot of such moments where I write the code not quite the way I want, but how ts makes me

99% that you have some kind of cant and possible problems (even if you run and "it works").
But inside the function there is also, say, such an intermediate typing (NodeListOf) with which I do not quite agree

you can agree or disagree as much as you like, the real data will not change from this. And here either the types match - and then the typescript helps you in development, or not - and then you have problems.
specifically in this line, no type should be set - the typescript already knows what you have there. just write const allLinksOnPage = document.querySelectorAll('img')
Do you think it's worth typing everything at all?

yes, except for places where types are extremely difficult to write and there is little profit from this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question