Why was 2015 TB145 detected just weeks before its closest approach, while so many other asteroids (which will make closer approach or farther approach) are detected years before? What made this asteroid difficult to detect?
2015 TB145 had an apparent magnitude of 20 when it was discovered. This is pretty faint and may account for it not being found earlier.
Most asteroids have a small inclination (angle relative to the ecliptic plane). Some of the early asteroid surveys didn't look at high inclinations, because it can take a long time to do a survey of the entire sky. These programs need to look at the same area of sky several times per year to detect which objects have moved, so they made a decision to limit the area they looked at. With increasing computing power and better CCDs it became possible to do full-sky surveys. These detection programs tend to get hardware upgrades every few years.
Pan-STARRS (which discovered 2015 TB145) does full-sky surveys and has been operational since 2010. So even with this inclination, Pan-STARRS has looked at the region of sky where 2015 TB145 was several times already without detecting it.
I bet that the high inclination, 40 degrees, is the explanation. Very few asteroids are, or have been found, at such high inclination. But I don't specifically know how asteroid searches work with inclinations.