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

Type Narrowing for hasAttribute and getAttribute #22238

Closed
TheColorRed opened this issue Feb 28, 2018 · 5 comments
Closed

Type Narrowing for hasAttribute and getAttribute #22238

TheColorRed opened this issue Feb 28, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@TheColorRed
Copy link

TypeScript Version: 2.7.1 and 2.8.0-insiders.20180127

Type guard should catch hasAttribute so that getAttribute doesn't return null.

In the following example, TS still says getAttribute could return null, however we previously checked for the attribute on the previous line.

let element = document.querySelector('#element')

if(element.hasAttribute('data-title')) {
  let el = element.getAttribute('data-title') // still returns "string | null"
}

Expected Result:

element.getAttribute('data-title') Return type should be string

Actual Result:

element.getAttribute('data-title') Return type is string | null

@weswigham
Copy link
Member

I believe this would be an (very complex, but possible) override of hasAttribute and getAttribute Element signatures over at the lib generator - Microsoft/TSJS-lib-generator.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 1, 2018

Duplicate of #18781 and #13086. I do nto think we want to add such overloads to the library, but you are welcomed to add them. @DanielRosenwasser has a sample how

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 1, 2018
@TheColorRed
Copy link
Author

TheColorRed commented Mar 1, 2018

@mhegazy Isn't has for Set and Map? Isn't that different than hasAttribute?

@weswigham
Copy link
Member

It's the same kinda situation; the types would look identical.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants