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

Stack overflow for subclass as static property #24194

Closed
rjamesnw opened this issue May 17, 2018 · 4 comments
Closed

Stack overflow for subclass as static property #24194

rjamesnw opened this issue May 17, 2018 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@rjamesnw
Copy link

rjamesnw commented May 17, 2018

TypeScript Version:
2.8 and 2.9

Search Terms:
Out of stack space

Code

namespace Test {
    export interface IType<TInstance = object> {
        new(...args: any[]): TInstance;
    }

    export function ClassFactory<TBaseClass extends IType, TClass extends IType, TFactory extends IType, TNamespace extends object>
        (namespace: TNamespace, base: IClassFactory & { $__type: TBaseClass }, getType: (base: TBaseClass) => [TClass, TFactory], exportName?: keyof TNamespace, addMemberTypeInfo = true)
        : TClass & TFactory & { $__type: TClass } {
        return null;
    }

    export function FactoryBase<TClass extends IType, TBaseFactory extends IType, TStaticProperties extends keyof TClass>
        (type: TClass, baseFactoryType: TBaseFactory) {
        return class FactoryBase extends type {
            static 'new'?(...args: any[]): any;
            static init?(o: InstanceType<TClass>, isnew: boolean, ...args: any[]): void;
        };
    }

    export namespace Loader {
        export var SomeClass = ClassFactory(Loader, void 0,
            (base) => {
                class SomeClass {
                    private x: number;
                    protected static readonly 'SomeClassFactory' = class Factory extends FactoryBase(SomeClass, null) {
                        static 'new'(): SomeClass { return null; }
                        static init(o: SomeClass, isnew: boolean) {
                            o.x = 1;
                        }
                    };
                }
                return [SomeClass, SomeClass["SomeClassFactory"]];
            }
        );

    }
}

Configuration used:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "suppressImplicitAnyIndexErrors": true,
    "traceResolution": true,
    "listFiles": true,
    "diagnostics": true,
    "forceConsistentCasingInFileNames": false,
    "noImplicitAny": true,
    "noEmitOnError": false,
    "removeComments": false,
    "noEmitHelpers": true,
    "declaration": true,
    "declarationDir": "wwwroot/js",
    //"typeRoots": [ "typings" ],
    "sourceMap": true,
    "target": "es5", // IE11=es5, Edge+ es2015=ES6
    "outDir": "wwwroot/js",
    "downlevelIteration": true,
    "rootDir": "Scripts", // *** location of the scripts ***
    "lib": [ "dom", "es6", "dom.iterable", "scripthost" ] // (target es5 but use es6 libs, since there is partial support of some es6 in es5; lib options: https://www.typescriptlang.org/docs/handbook/compiler-options.html)
  },
  "include": [
    "typings/**/*.ts",
    "Scripts/testscript.ts"
  ]
}

Compiled using this line:
tsc.exe --project "{path too source}\tsconfig.json" --locale en-US

Expected behavior:
Good behavior: Not out of stack space error.

Actual behavior:
Bad: Out of stack space error.

Playground Link:
https://goo.gl/UHStFt

This works PERFECT when this is commented out:

    "declaration": true,
    "declarationDir": "wwwroot/js",

I had to spend a lot of time whittling down the production source (many, many, files) down to this test. The production code this is taken from compiles AND runs PERFECT! Everyone is a happy camper (this code also compiles ok when declarations are turned off), BUT when I switch on declaration file generation, everything craps out (and the campers are not happy anymore). ;)

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 17, 2018
@ghost
Copy link

ghost commented May 17, 2018

Simplified this to:

class C {
    static D = class extends C {};
}

And compile with --declaration.

@ghost ghost changed the title Out of stack space compiling code. Stack overflow for subclass as static property May 17, 2018
@mhegazy mhegazy added this to the TypeScript 2.9.1 milestone May 17, 2018
@rjamesnw
Copy link
Author

Thank you! I didn't have time to break it down further because I had to leave for awhile. I was going to come back and break it down myself later if someone else didn't figure it out by now, so thanks. ;) Can you think of a workaround? I really need this to work asap lol. :)

@ghost
Copy link

ghost commented May 17, 2018

@rjamesnw It should be fixable by providing a type annotation on SomeClassFactory, e.g. { new(): SomeClassFactoryInstance }.

@rjamesnw
Copy link
Author

rjamesnw commented May 17, 2018

Unfortunately, I can't do that since the instance is inferred from a combination of the current class and a base factory type as passed into a function. This is not known in advance unless I create more messy code and move the inner factory code to the end of the class; however, that also won't work because I cannot access private properties on the class unless it is nested (hence why it is inside the class in the first place). ;) I guess I'll have to try something else or wait for a fix. Also, I need the static type properties not to be erased in the process. The static factory is returned, the instance means nothing really.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 17, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants