Feature request: Restrict variables could be used in function scope

Topics: General, Language Specification
Apr 8, 2013 at 4:55 AM
Problem.
we have the following code:
function copy(array:any[], startIndex:number, count:number):any[]{
   var arrayCopy = new Array(count);
   
   for(var i = 0; i < length; i++){
        arrayCopy[startIndex + i] = array[startIndex + i];
   }
   return arrayCopy;
}
The bug: we use never declared 'length' variable instead of 'count' parameter.
But the code above compiles without any warning, cause the global scoped 'length' variable declared in lib.d.ts
Also, lib.d.ts exports global scoped commonly used 'item' name, and etc.

It would be nice we could somehow specify the function/method should use only parameters and locally declared variables.
Apr 8, 2013 at 4:04 PM
Edited Apr 8, 2013 at 4:04 PM
It would be useful to see a solution to this, because it is a common error.

E.g
class Foo {
 private name:string = "foo";
  getName(){
      // Compiles but returns the name of the browser window
      // rather than "foo", because we forgot "this.name"
    return name;
 }  
}
The same is true for very common variable names such as location, length, parent, status, self, top...

Perhaps if we require that global variables be prefixed with "window"?
  getName(){
    return name; // Compile-time error: "Undeclared variable"
    return this.name; // ok "foo"
    return window.name; // ok "whatever"
 }