Skip to content

Add base class in lib.scripthost.d.ts for Automation objects #18407

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

Open
zspitz opened this issue Sep 12, 2017 · 1 comment
Open

Add base class in lib.scripthost.d.ts for Automation objects #18407

zspitz opened this issue Sep 12, 2017 · 1 comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@zspitz
Copy link
Contributor

zspitz commented Sep 12, 2017

TypeScript Version: 2.5.0 / nightly (2.6.0-dev.20170902)

Code

// in lib.scripthost.d.ts
class AutomationObject<T> {
  private constructor();
  private typekey: AutomationObject<T>;
}

// in activex-excel.d.ts
declare namespace Excel {
    class Application extends AutomationObject<Application> {
        Workbooks: any;
    }
}

interface ActiveXObject {
    new(progid: 'Excel.Application'): Excel.Application;
}
    
// usage
let x: Excel.Application = new ActiveXObject('Excel.Application');

This currently wouldn't compile, because of the private constructor in the base class; but pending resolution of #18283, it would be possible.

This would prevent assigning structurally matching objects to the inheriting types:

// Compiler error
x = {
    Workbooks: [];
};

and also prevent using new to create a new instance of the inheriting type:

// Compiler error
x = new Excel.Application();

I've opened an issue to define similar behavior for VarDate and SafeArray<T> in lib.scripthost.d.ts. If AutomationObject<T> is defined, then it could be used in lib.scripthost.d.ts as a base class for these types as well:

declare class VarDate extends AutomationObject<VarDate> { }
declare class SafeArray<T> extends AutomationObject<SafeArray<T>> { }
@zspitz zspitz changed the title Add base class for Automation objects in lib.scripthost.d.ts Add base class in lib.scripthost.d.ts for Automation objects Sep 12, 2017
@mhegazy mhegazy added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript labels Sep 12, 2017
@zspitz
Copy link
Contributor Author

zspitz commented Sep 13, 2017

On further consideration, this suggestion would have value for a flat inheritance hierarchy, where all the declared classes inherit directly from AutomationObject<T>.

However, when one declared class inherits from another, inheriting from AutomationObject<T> would not prevent assigning an instance of the base class to a subclass typed variable or parameter:

declare class BaseAutomation extends AutomationObject<BaseAutomation> { }
declare class DerivedAutomation extends BaseAutomation { }
let x: BaseAutomation = ...
let y: DerivedAutomation = x; // both have the same private key, so it's a valid assignment

In the real world, this might not be an issue, because DerivedAutomation would probably have some additional members that would prevent this assignment.

@weswigham weswigham added the In Discussion Not yet reached consensus label Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants