Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should static properties in class definition trigger build error when missing /// reference? #13389

Closed
jeffsim opened this issue Jan 10, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jeffsim
Copy link

jeffsim commented Jan 10, 2017

I have a Typescript project which generates a single bundled .js library for use in client-side webapps. I am using namespaces and /// references. I would like to get design/build-time errors when one file in the library depends on another file and there is no /// reference present; otherwise, it won’t be discovered until runtime. The compiler catches the missing reference in a number of cases, but not if a static variable is declared and assigned in a class's definition.

e.g.: with the following, I get a nice compile-time error:

// AClass.ts
namespace test {
    export class AClass extends ZClass {
        constructor() {
            super();
        }
    }
}
...
// ZClass.ts
namespace test {
    export class ZClass {
        constructor() {
        }
    }
}

TSC complains that error TS2690: A class must be declared after its base class as expected, and adding /// <reference path=’zclass.ts’ fixes it, again as expected.

The problem is when you instantiate a static variable, it doesn’t seem to get caught at build time. e.g., change the above code as follows:

// AClass.ts
namespace test {
    export class AClass {
        static z = new ZClass();
        constructor() {
        }
    }
}
...
// ZClass.ts
namespace test {
    export class ZClass  {
        constructor() {
        }
    }
}

When you compile now, no build errors are emitted; but when you run you get "test.AClass is not a constructor" because ZClass isn’t defined at this point in the bundled .js file; that definition occurs after AClass. If you switch the object names, then it works. Or, if you add /// <reference path="ZClass.ts"/> to AClass.ts, it works.

Should TSC output a build error in this scenario? My concern is that it's easy to miss adding one of those references.

@RyanCavanaugh
Copy link
Member

Duplicate #12673 ?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Sep 18, 2019
@RyanCavanaugh
Copy link
Member

Correctly errors now assuming you've configured your build correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants