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
exporttypePoint2D={x: number,y: number};exporttypePoint3D=Point2D&{z: number};exporttypePoint=Point2D|Point3D;exportfunctiondraw(p: Point){// This check should not be an error!if(p.z!=undefined){console.log("Point3D");}else{console.log("Point2D");}}letp1: Point={x: 1,y: 2,z: 3};draw(p1);// We can quickly address your report if:// - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!// - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.// - The incorrectness of the behavior is readily apparent from reading the sample.// Reports are slower to investigate if:// - We have to pare too much extraneous code.// - We have to clone a large repo and validate that the problem isn't elsewhere.// - The sample is confusing or doesn't clearly demonstrate what's wrong.
π Actual behavior
Property 'z' does not exist on type 'Point'.
Property 'z' does not exist on type 'Point2D'.(2339)
π Expected behavior
Checking if the property is defined should not be an error.
The text was updated successfully, but these errors were encountered:
Okay. It seems to be a duplicate which I didn't find that earlier in my search.
Please note that @RyanCavanaugh mentioned the following:
The "lack" of this feature is very much intentional; this pattern is not safe to use because object types aren't closed, so the existence of a value in some property slot is really not a solid indication that anything correct is happening. The in operator is granted an exception to this because there's nothing else you might be intending to do with the in operator.
Even though, the claim is valid, that's possibly not a common case, and additional type checks can be performed if the code is doing something weird.
Using in operator works fine, but it has certain limitations such as if the original property is renamed, the conditional check might not be updated properly.
Bug Report
π Search Terms
Property Definition Check
Property Undefined Check
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
π Expected behavior
Checking if the property is defined should not be an error.
The text was updated successfully, but these errors were encountered: