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

Suggestion: Merge ambient function and class declarations with the same name. #2959

Closed
rbuckton opened this issue Apr 29, 2015 · 2 comments · Fixed by #32584
Closed

Suggestion: Merge ambient function and class declarations with the same name. #2959

rbuckton opened this issue Apr 29, 2015 · 2 comments · Fixed by #32584
Labels
Suggestion An idea for TypeScript

Comments

@rbuckton
Copy link
Member

Summary

To better support subclassing built-ins, and still support built-ins and pre-ES6 "classes" that can also be called as functions, we should be able to merge the call signatures of ambient function declarations with the static side of a class declaration.

Current state

Currently, if you want to declare an ambient "class" that has both call and construct signatures, you need to use an interface-interface-var pattern, as follows:

// instance-side interface type
interface Array<T> { /*...*/ }

// static-side interface type
interface ArrayConstructor {
  <T>(length: number): T[];
  new <T>(length: number): T[];
  /* ... */
}

// value declaration for constructor
declare var Array: ArrayConstructor;

This is a problem, however as "classes" defined using this pattern cannot be used in the extends clause of a class declaration in TypeScript.

Proposal

Merge ambient function and class declarations with the same name. Ambient function declarations with the same name as an ambient class declaration in the same lexical scope would contribute their call signatures to the static side of the class declaration. For example:

// lib.d.ts
declare function Array<T>(length: number): T[];
declare class Array<T> {
  constructor(length: number);
}

// app.ts
var ar1 = Array(10); // T[]
var ar2 = new Array(10); // T[]

Out of scope

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

Looks like the ES7/ES Later proposal for callable constructors would cover the part of definition it, we will only need to allow redefinition classes.

@rbuckton do you agree?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Dec 10, 2015
@mhegazy mhegazy removed the Needs More Info The issue still hasn't been fully clarified label Feb 20, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2016

closing in favor of callable constructors.

@mhegazy mhegazy closed this as completed Feb 20, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants