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 narrowed non-nullness lost in array method callback #19778

Closed
aaronbeall opened this issue Nov 6, 2017 · 2 comments
Closed

Type narrowed non-nullness lost in array method callback #19778

aaronbeall opened this issue Nov 6, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@aaronbeall
Copy link

aaronbeall commented Nov 6, 2017

TypeScript Version: 2.5.3

Code

declare const obj: { props?: string[] }

obj.props // string[] | undefined
if (obj.props) {
    obj.props // string[]
    obj.props.forEach(p =>
        obj.props // string[] | undefined <-- Expected string[]
    )
}

Expected behavior:
obj.props inside forEach callback to be string[]

Actual behavior:
obj.props inside forEach callback goes back to possibly undefined, leading to errors if not again type guarded

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

Please see #10982

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 6, 2017
@aaronbeall
Copy link
Author

aaronbeall commented Nov 6, 2017

Thanks, seems TS was telling me something I missed after all :)

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

2 participants