Skip to content

Checking whether a property is defined or not using the dot notation should not be an error. #42167

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

Closed
biswasxx08 opened this issue Dec 31, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@biswasxx08
Copy link

biswasxx08 commented Dec 31, 2020

Bug Report

πŸ”Ž Search Terms

Property Definition Check
Property Undefined Check

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export type Point2D = { x: number, y: number };
export type Point3D = Point2D & { z: number };
export type Point = Point2D | Point3D;

export function draw(p: Point) {
  // This check should not be an error!
  if (p.z != undefined) { console.log("Point3D"); }
  else { console.log("Point2D"); }
}

let p1: 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.

@andrewbranch
Copy link
Member

Duplicate of #39065

@andrewbranch andrewbranch marked this as a duplicate of #39065 Dec 31, 2020
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Dec 31, 2020
@biswasxx08
Copy link
Author

biswasxx08 commented Jan 1, 2021

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.

See e.g. #37343 (comment)

Originally posted by @RyanCavanaugh in #39065 (comment)

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.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants