You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Narrowing only occurs on specific syntax. By using a type assertion the compiler is losing the context to the variable and can't narrow it anymore. You can narrow a variable, such as addons[0]?.product, but you can't narrow an expression like addons[0]?.product as Product. I'm sure I've seen an issue about this already, but I can't find it anymore right now. (edit: Semi related: #34974 (comment))
A simple workaround is to store type-asserted value in a variable and then narrow the variable:
π Search Terms
Optional Chaining, as Operator, Type Narrowing
π Version & Regression Information
v4.6.2, 'Nightly' version as well
β― Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgtgQwObQLxQN4CgpQGYD2ATosAM4BcUZwRAlgHZIDaAumwNxYC+WWokKAAUiBACYBXAMbAo6bLjqIUlWMohsoAHygMJAG31de-cNACCUqRDJliIOZhMCLYsQQaOFUMKMkyqEXFpWR1La1t7bV0DIx4+PAkGGToPKCkACwgpAGsACgQ3D1VzIoY2AEpMHCg6PCg8grKyZgAGVgB+ADpfYJkoBDJhPxCK7qVkG279CCZgDKrvXCli2Tw6Ihp4Sccm9wYW9p6R-sHhvuAKromVNtYuXF5uIA
π» Code
π Actual behavior
Errors in code: Object is possibly 'null'
π Expected behavior
No errors
Additional note
If you remove 'Accessory' type at all and remove the 'as' operator, everything will work fine
The text was updated successfully, but these errors were encountered: