We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: 3.6.3
Search Terms: object is possibly undefined
Code
const foo = (bar: { baz?: { name: string } }) => { const isBarBazDefined = !!bar.baz; if(isBarBazDefined) console.log(bar.baz.name); }
Expected behavior: Typescript should not give an 'object is possibly undefined' error.
Actual behavior: Typescript does give an 'object is possibly undefined' error.
Playground Link: http://www.typescriptlang.org/play/?ssl=4&ssc=2&pln=1&pc=1#code/MYewdgzgLgBAZiEMC8MAUAjAhgJwFwwDeM2AXgPwHFhYC2ApgdDgJZgDmMAvtwJQoA+IgCgYYmKEiwWEAEK55pACL04begBMUMAIQ7sOAHRkA3KPEs4aGfJyKVasJv6SIIADb1D7kO0y5jLFJDGgZeMy4gA
Related Issues: Couldn't find any.
I realise this is very simple case, but I haven't been able to find an existing issue or FAQ entry on the subject...
The text was updated successfully, but these errors were encountered:
This is a duplicate of #12184 - narrowing results cannot be assigned to an identifier and reused.
Sorry, something went wrong.
Thanks. Is there any recommended workaround, besides again doing the check ?
@barroudjo The quick and easy way is a ! assertion:
!
const foo = (bar: { baz?: { name: string } }) => { const isBarBazDefined = !!bar.baz; if(isBarBazDefined) console.log(bar.baz!.name); }
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Successfully merging a pull request may close this issue.
TypeScript Version: 3.6.3
Search Terms: object is possibly undefined
Code
Expected behavior:
Typescript should not give an 'object is possibly undefined' error.
Actual behavior:
Typescript does give an 'object is possibly undefined' error.
Playground Link:
http://www.typescriptlang.org/play/?ssl=4&ssc=2&pln=1&pc=1#code/MYewdgzgLgBAZiEMC8MAUAjAhgJwFwwDeM2AXgPwHFhYC2ApgdDgJZgDmMAvtwJQoA+IgCgYYmKEiwWEAEK55pACL04begBMUMAIQ7sOAHRkA3KPEs4aGfJyKVasJv6SIIADb1D7kO0y5jLFJDGgZeMy4gA
Related Issues:
Couldn't find any.
I realise this is very simple case, but I haven't been able to find an existing issue or FAQ entry on the subject...
The text was updated successfully, but these errors were encountered: