Skip to content

Indexing into a select .options attribute doesn't return an HTMLOptionElement #14522

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
jasongrout opened this issue Mar 7, 2017 · 7 comments
Labels
Bug A bug in TypeScript

Comments

@jasongrout
Copy link

TypeScript Version: 2.2.1

Code

let x = document.createElement('select');
x.appendChild(document.createElement('option'));

let z = x.options[0];
// Error, z is an Element, not an HTMLOptionElement
let value = z.value;

(http://www.typescriptlang.org/play/#src=let%20x%20%3D%20document.createElement('select')%3B%0D%0Ax.appendChild(document.createElement('option'))%3B%0D%0A%0D%0Alet%20z%20%3D%20x.options%5B0%5D%3B%0D%0A%2F%2F%20Error%2C%20z%20is%20an%20Element%2C%20not%20an%20HTMLOptionElement%0D%0Alet%20value%20%3D%20z.value%3B%0D%0A)

Expected behavior:

z should be typed as an HTMLOptionElement, allowing access to a .value property.

Actual behavior:

z is typed as an Element

I think this error was exposed when I moved my code from TS 2.1 to 2.2.

@jasongrout
Copy link
Author

Possibly related to #9334 and #8220

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds Bug A bug in TypeScript and removed Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Mar 7, 2017
@jasongrout
Copy link
Author

@RyanCavanaugh
Copy link
Member

@mhegazy / @zhengbli seems like we should have this?

interface HTMLCollectionOf<T extends Element> extends HTMLCollection {
    [index: number]: T; // <-- add
    item(index: number): T;
    namedItem(name: string): T;
}

@jasongrout
Copy link
Author

(For whoever may be reading this...) A work around for now is to use x.options.item(0), or of course you can cast the value.

@evmar
Copy link
Contributor

evmar commented Apr 8, 2017

@RyanCavanaugh looking at the blame for lib.dom.d.ts, it looks like it used to have that and an update to it lost it. When we updated Google to TS 2.2 we had to work around foo[i] with foo.item(i) in a bunch of places.

@jasongrout
Copy link
Author

jasongrout commented Sep 12, 2017

My example above now seems to work - I think this can be closed, at least for the select element. I'm not sure if other HTMLCollectionOf things work, since the change mentioned above has not been done.

@jasongrout
Copy link
Author

@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
Projects
None yet
Development

No branches or pull requests

3 participants