Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

New Rule: no-use-before-initialization #624

Closed
adidahiya opened this issue Aug 30, 2015 · 5 comments
Closed

New Rule: no-use-before-initialization #624

adidahiya opened this issue Aug 30, 2015 · 5 comments

Comments

@adidahiya
Copy link
Contributor

Suggested in microsoft/TypeScript#2234

If the typechecker knows that a value is undefined at a certain point in the program, this rule would disallow:

  1. accessing properties or indexing into an object or array

    var arr: number[];
    var obj: any;
    arr.push(1);
    obj.foo = "bar";
  2. returning a primitive value or using it in a RHS expression

    function dd(): number {
      var x: number;
      return x;
    }
    
    function ddd() {
      var x: number;
      x++;
    }
@pawciobiel
Copy link

Would it make sense to fix those too?

var a: number = 2;
var b: number;
var c: number = null;  // error
console.log(a + b);  // error
console.log(b + c);  // error
console.log(a + c);  // error

@adidahiya
Copy link
Contributor Author

@pawciobiel yep, I think that would fall under the category of RHS (right-hand-side) expressions

@adidahiya
Copy link
Contributor Author

blocked on #680 I believe

@andy-hanson
Copy link
Contributor

no-use-before-declare seems to do this.

@adidahiya
Copy link
Contributor Author

In v5.0 with the change to be a typed rule, no-use-before-declare does seem to do this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants