Typescript: Report use before definitely defined errors

Created on 11 Oct 2015  路  12Comments  路  Source: microsoft/TypeScript

This compiled code will throw a ReferenceError exception by native javascript engine if Class syntax is implemented. It means that TS compiler generate the invalid code. Should be disallow the use of a class before definition like a Let and Const Declaration because Class Declaration does not specified as hoisting target by es6 spec.

a; // ts: pass.
   // js: pass.
var a;
b; // ts: compile error
   // js: runtime error(ReferenceError).
let b;
c; // ts: compile error
   // js: runtime error(ReferenceError).
const c = void 0;
f(); // ts: pass.
     // js: pass.
function f(){}

new Foo(); // ts: pass.
           // js: runtime error(ReferenceError)!
           // Class Declaration is not hoistable.
           // should be a compile error before throw a runtime error.
class Foo {}

http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-statements-and-declarations
http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement
http://www.2ality.com/2015/02/es6-classes-final.html

I think, this issue is different from #2854 because this issue is based on spec.

Committed Fixed Suggestion

Most helpful comment

@mhegazy will refresh his PR and implement this for extends clauses and import declarations

All 12 comments

I think @vladima's CFA work might catch this.

What is CFA? Latest commit does not detect this error.

What is CFA

Code Flow Analysis. Fancy term for code reach-ability checking.

Latest commit does not detect this error.

The work is not on master. Here is one PR : https://github.com/Microsoft/TypeScript/pull/4788 ... there might be others :rose:

@basarat thanks! I tested on #4788 but could not detect for now. I think reach-ability check and declaration(and evaluation order) check are different.

this is not CFA; CFA is detecting unreachable code, e.g. a statement after a return. what this issue is about is Definite Assignment Analysis. you want to know that a variable is definitely assigned to before the first use site.

I think, this issue is different from #2854 because this issue is based on spec.

the JS spec talks about runtime errors. these can not all be detected at design time easily. issue #2854 is not the right issue. i will update this issue title to reflect the request.

@mhegazy will refresh his PR and implement this for extends clauses and import declarations

@sheetalkamat please include this in your PR in: https://github.com/Microsoft/TypeScript/pull/4343

Just because clicking through several links to find this issue is becoming a massive pain.

This is an issue for TypeScript to:

  • report errors on use before definition
  • report errors on use before declaration
  • report errors on use before define
  • report errors on use before declare
  • report errors on use before initialization

Hopefully that makes this more Google-friendly.

Hi, just to point out that as of v2.1.4 this is still an issue, although this issue has the label commited, which I found a bit confusing.

Committed: Someone from the TypeScript team will fix this bug or implement this feature

more about labels at: https://github.com/Microsoft/TypeScript/wiki/FAQ#what-do-the-labels-on-these-issues-mean

Thanks @mhegazy, that explains my confusion.

Should be fixed in latest.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

dlaberge picture dlaberge  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

wmaurer picture wmaurer  路  3Comments

weswigham picture weswigham  路  3Comments