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

Abstract Constructor Assignability Not Restricted Enough #4190

Closed
aozgaa opened this issue Aug 6, 2015 · 9 comments
Closed

Abstract Constructor Assignability Not Restricted Enough #4190

aozgaa opened this issue Aug 6, 2015 · 9 comments
Labels
Bug A bug in TypeScript

Comments

@aozgaa
Copy link
Contributor

aozgaa commented Aug 6, 2015

Consider the following code snippet:

abstract class A {}
class B extends A {}

var A2: new () => A;
var BCtor: typeof B;

A2 = A;
BCtor = A1;

The first assignment should fail because A2 is new-able (it isn't an abstract constructor type), but no error is reported.

EDIT: simplified the sample.

@aozgaa aozgaa added the Bug A bug in TypeScript label Aug 6, 2015
@aozgaa aozgaa self-assigned this Aug 6, 2015
@tinganho
Copy link
Contributor

tinganho commented Aug 6, 2015

If this is a bug how do I do this #2947 (comment)?

@RyanCavanaugh I have another question about abstract classes. I wonder how I define a function that accepts a derived abstract class that is not abstract and the function should also "new" it.

The following doesn't work:

function testDerived<T extends typeof ComposerComponent>(DerivedClass: T) {
   new DerivedClass(); // error
}
function testDerived(DerivedClass: typeof ComposerComponent) {
   new DerivedClass(); // error
}

And @RyanCavanaugh suggested:

function testDerived(DerivedClass: new() => ComposerComponent) {

I think that there should be a modifier for declaring derivative of an abstract class that is not abstract.

var A2: new () => implementationof A;

@aozgaa
Copy link
Contributor Author

aozgaa commented Aug 6, 2015

Let me check my understanding. Suppose our class hierarchy looks like

    A
   / \
  B   C 

where A is abstract but B,C are not.

Then you want to have an object AA which has a type that allows

AA = B;
AA = C;
new AA;

but forbids

AA = A;

If my understanding is correct, that's precisely the capability that should be provided by
var A2: new () => A; in the example above. Right now, the forbidden condition isn't forbidden.

If I'm misunderstanding, can you clarify where I'm going astray?

@tinganho
Copy link
Contributor

tinganho commented Aug 6, 2015

If my understanding is correct, that's precisely the capability that should be provided by
var A2: new () => A; in the example above. Right now, the forbidden condition isn't forbidden.

This is the only way that I know of that can do the capability we are talking about.

Though personally I also feel that this is a bug. Though I want to have the capability to declare a non abstract class declaration type. If you fix this bug, then I don't know how to do it(except using any).

@aozgaa
Copy link
Contributor Author

aozgaa commented Aug 6, 2015

Could you clarify what you mean by "a non abstract class declaration type"?

@tinganho
Copy link
Contributor

tinganho commented Aug 6, 2015

@aozgaa don't know the real name of it. but just to clarify my meaning:

abstract class A {}
typeof A // abstract class declaration type
class B extends A {}
typeof B //non-abstract class declaration type

@aozgaa
Copy link
Contributor Author

aozgaa commented Aug 7, 2015

I see. So what's missing from the constructor function type A2 above? Do you want to also have the static-side of A encoded into A2? What's an example of a use-case of the "non-abstract class declaration types" when dealing with abstract classes?

@tinganho
Copy link
Contributor

tinganho commented Aug 7, 2015

Do you want to also have the static-side of A encoded into A2

I think this is already handled?

What's an example of a use-case of the "non-abstract class declaration types" when dealing with abstract classes

As I mentioned in the quoted comments above. I want pass a "non-abstract class declaration type" to a function and instantiate using new.

@aozgaa
Copy link
Contributor Author

aozgaa commented Aug 7, 2015

@tinganho

Today, the following code will work:

        class A {}

        function factory(AA: typeof A) {
            return new AA;
        }

@tinganho
Copy link
Contributor

tinganho commented Aug 8, 2015

@aozgaa thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants