I Why can I assume the force to be constant in this interval?

If I have force, or any function f(z), I was told that I can assume it to be constant only in the interval dz.

However, in this case, I had to calculate the work done by the spring force as a function of y

Over here, I assumed the spring force, which is a function of its elongation x (F = -kx) to be constant in the interval dy and integrated and this gave me the correct answer

I want to know why the error vanished over here. Shouldn't spring force only be constant in the interval dx and not dy?

I also want to know, in general, if I have a function, how to decide whether it is constant in some particular interval/in which cases the error will vanish as I take the limit and integrate. Or are forces/functions constant for any infinitesimal intervals such as Rdθ, dy/cosθ, dz etc etc.?

It is assumed that dz is infinitesimally small and that f is continuous within the z axis segment that dz spans. Then you can assume that the function is at it's segment maximum, minimum, average, or any of its segment values. The differences should be so small that it doesn't change the integral value significantly.

If I have force, or any function f(z), I was told that I can assume it to be constant only in the interval dz.

However, in this case, I had to calculate the work done by the spring force as a function of y

Over here, I assumed the spring force, which is a function of its elongation x (F = -kx) to be constant in the interval dy and integrated and this gave me the correct answer

I want to know why the error vanished over here. Shouldn't spring force only be constant in the interval dx and not dy?

I also want to know, in general, if I have a function, how to decide whether it is constant in some particular interval/in which cases the error will vanish as I take the limit and integrate. Or are forces/functions constant for any infinitesimal intervals such as Rdθ, dy/cosθ, dz etc etc.?

You are touching on the very basics of Calculus here. Long story short: you start with a finite step in x (δx) and that gives you a change δy. The Limit of the value of the work done dW assumes (justifiably here) that θ doesn't change as δx →0
There has been loads and loads written about this sort of thing and, if you get a Calculus Intro text book, you can find out when that sort of step is justified and when it's not. You could try posting a similar question on the Maths Forum and see if you get a better answer from a Mathematician who can sum it up in less than several pages of text book.

You are touching on the very basics of Calculus here. Long story short: you start with a finite step in x (δx) and that gives you a change δy. The Limit of the value of the work done dW assumes (justifiably here) that θ doesn't change as δx →0
There has been loads and loads written about this sort of thing and, if you get a Calculus Intro text book, you can find out when that sort of step is justified and when it's not. You could try posting a similar question on the Maths Forum and see if you get a better answer from a Mathematician who can sum it up in less than several pages of text book.