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: a precise return type of Node.appendChild() #12048

Closed
ericzhangjx opened this issue Nov 4, 2016 · 2 comments
Closed

Suggestion: a precise return type of Node.appendChild() #12048

ericzhangjx opened this issue Nov 4, 2016 · 2 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@ericzhangjx
Copy link

TypeScript Version: 2.0.6

Code
From lib.es6.d.ts:

interface Node extends EventTarget {
  // ...
  appendChild(newChild: Node): Node;
  // ...
}

Test case:

document.createElement('div').appendChild(document.createElement('span'));

Expected behavior:
According to Node.appendChild() - MDN:

Syntax
var aChild = element.appendChild(aChild);
The returned value is the appended child.

The return type should be the type of the element being appended, in this case, HTMLSpanElement.

Actual behavior:
The appendChild(document.createElement('span')) just return with the type Node.

Suggestion
I wonder if it would be better using Generics to define the return type of appendChild(), such as:

interface Node extends EventTarget {
  appendChild<T>(newChild: T): T;
}
@aluanhaddad
Copy link
Contributor

interface Node extends EventTarget {
  appendChild<T extends Node>(newChild: T): T;
}

makes sense to me.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Nov 4, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 7, 2016

PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.

@mhegazy mhegazy added this to the Community milestone Dec 7, 2016
@mhegazy mhegazy added the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Jan 9, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.2, Community Jan 9, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 2, 2017
@zhengbli zhengbli removed the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Feb 3, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants