On the face of it, it sounds like you're asking us to explain why some unknown person might have performed a type assertion in some unspecified TypeScript code for some unspecified TypeScript version. Not sure how someone could really help you there.
But luckily the magic of search engines has divined that you're referring to this tutorial for writing your own language service plugin in TypeScript, written by @RyanCavanaugh.
The reason seems to be that the key k is being inferred as string, and not as keyof ts.LanguageService, so the compiler rejects indexing into proxy and oldLS with k, unless you do some sort of assertion.
I'm not exactly sure why this is happening; I think that since TypeScript v2.1.4, the variable a in for a in b should have the type keyof typeof b.
For example:
function hmm<T>(t: T): void {
for (const k in t) { // k is inferred as keyof T
const keyofT: keyof T = k; // no error
}
}
But in the code you included it infers only as string, for some reason ( maybe someone in the know can explain).
Once TypeScript fails to infer something you yourself know to be true, you can usually force the compiler to bend to your will by an annotation (in the case of bivariant parameters), or a type assertion.
In my case, I'd probably have done this instead:
type K = keyof ts.LanguageService
for (const k in oldLS) {
proxy[k as K] = function() {
return oldLS[k as K].apply(oldLS, arguments);
}
}
but that's a judgment call.
Hope that helps; good luck.