Typescript: Class 'x' used before its declaration

Created on 11 Aug 2017  ·  4Comments  ·  Source: microsoft/TypeScript

##

TypeScript Version: 2.4.1
Visual Studio Version: 14.0.25420.01 Update 3

File: ParallelogramF.ts
namespace JMath { export class ParallelogramF extends Polygon { ... } }
File: Polygon.ts
namespace JMath { export class Polygon { } }

Expected behavior:
Class Polygon must be known correctly.

Actual behavior:
Error Occurs: Class Polygon used before its declaration.

This occurs only when 'Combine JavaScript output into file' checkbox in project properties is checked and some 'xxx.js' file name is specified in front of it.

Solution:
1- When I change the name of file "ParallelogramF.ts" to "ZParallelogramF.ts" problem solves and it compiles correctly!!!!
2- When I uncheck 'Combine JavaScript output into file' checkbox it compiles correctly!!!!

Changing place of 'TypeScriptCompile Include="ParallelogramF.ts"' in ".csproj" file of the project to the last item inside its parent tag does not help.

Question

Most helpful comment

@arashebrahimi Try adding
/// <reference path="Polygon.ts"/>
to the very top of your ParallelogramF.ts file

There may be a "newer" way of doing that, but that let's the compiler know to compile Polygon.ts before ParallelogramF.ts when compiling into a single file

All 4 comments

You will note that in the cases where you get an error, the compiled code will not run.

Thank you for response,

I can not find the relation between this issue and your response?!!!
It is obvious that when there is an error, the compiled code will not run!!!!!!!!!

But I talked about incorrect 'error' when I compile!!!!
Where I talked about 'run'???

I said there is a bug when I compile, It should be compiled without error, but I get 'incorrect' error!!!!

@arashebrahimi Try adding
/// <reference path="Polygon.ts"/>
to the very top of your ParallelogramF.ts file

There may be a "newer" way of doing that, but that let's the compiler know to compile Polygon.ts before ParallelogramF.ts when compiling into a single file

I don't understand how this is expected behavior. The issue is that the child class isn't being recognized as such within the same namespace -- which should, in theory, eliminate the need to explicitly call out the reference. The compiler should _know_ to compile the base class prior to any and all child classes.

What I believe @arashebrahimi is stating is that it appears to be an alphabetization issue where child classes that come after the base class (in file name) alphabetically recognize the inheritance relationship, but those that come prior don't. I've found that this issue doesn't exist for interfaces, however.

I'm able to reproduce this issue with TS 2.5.3 in Visual Studio Version 15.4.5.

Was this page helpful?
0 / 5 - 0 ratings