Skip to content

in type signatures, cannot refer to class defined as static anonymous class #7421

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

Closed
kenhahn85 opened this issue Mar 7, 2016 · 4 comments
Closed
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@kenhahn85
Copy link

TypeScript Version:

nightly (1.9.0-dev.20160307)

Code

// A self-contained demonstration of the problem follows...
export abstract class CaseClass {
  constructor(...args: any[]) {}
}

abstract class TestCaseClass extends CaseClass {
  static A = <typeof CaseClass> class extends TestCaseClass {
    v: number;
    constructor(v: number) {
      super();
      this.v = v;
    }
  };
  static B = <typeof CaseClass> class extends TestCaseClass {
    v: string;
    constructor(v: string) {
      super();
      this.v = v;
    }
  };
}

function test(v: TestCaseClass.A) {
  return v.v;
}

Expected behavior:
I would expect this to compile.

Actual behavior:
A failure in the compilation of test:

error TS2503: Cannot find namespace \'TestCaseClass\'.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 7, 2016
@RyanCavanaugh
Copy link
Member

Seems like something we could do. In the meantime, you can rewrite this to something that is basically equivalent:

export abstract class CaseClass {
    constructor(...args: any[]) { }
}

abstract class TestCaseClass extends CaseClass {
}
namespace TestCaseClass {
    export class A extends TestCaseClass {
        v: number;
        constructor(v: number) {
            super();
            this.v = v;
        }
    }

    export class B extends TestCaseClass {
        v: string;
        constructor(v: string) {
            super();
            this.v = v;
        }
    };
}

function test(v: TestCaseClass.A) {
    return v.v;
}

@DanielRosenwasser
Copy link
Member

You could also write something like this:

export abstract class CaseClass {
  constructor(...args: any[]) {}
}

abstract class TestCaseClass extends CaseClass {
  static A = class extends TestCaseClass {
    v: number;
    constructor(v: number) {
      super();
      this.v = v;
    }
  };
  static B = class extends TestCaseClass {
    v: string;
    constructor(v: string) {
      super();
      this.v = v;
    }
  };
}

function test(v: typeof TestCaseClass.A.prototype) {
  return v.v
}

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Aug 12, 2017
@RyanCavanaugh
Copy link
Member

We might potentially turn class names into namespaces if this turns out to be a common pattern, but for now it doesn't look like there's much demand for it

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 2, 2019
@RyanCavanaugh
Copy link
Member

Haven't gotten any other feedback on this. For non-abstract classes you can write InstanceType<typeof TestCaseClass["B"]> (probably worth aliasing) as an alternate way of referring to those types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants