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

Java #60104

Closed
Juan5212 opened this issue Sep 30, 2024 · 0 comments
Closed

Java #60104

Juan5212 opened this issue Sep 30, 2024 · 0 comments
Labels
Duplicate An existing issue was already created

Comments

@Juan5212
Copy link

TypeScript has two narrowing-related behaviors that are both intentional. Please do not log additional bugs on this; see #9998 for more discussion.

The first is that narrowings are not respected in callbacks. In other words:

function fn(obj: { name: string | number }) {
  if (typeof obj.name === "string") {
    // Errors
    window.setTimeout(() => console.log(obj.name.toLowerCase());
  }
}

This is intentional since the value of obj.name "could" change types between when the narrowing occurred and when the callback was invoke. See also #11498

The second is that function calls do not reset narrowings. In other words:

function fn(obj: { name: string | number }) {
  if (typeof obj.name === "string") {
    console.log("Here");
    // Does not error
    console.log(obj.name.toLowerCase());
  }
}

This is intentional behavior, even though console.log could have mutated obj. This rule is consistently applied, even with the function is in-principle inspectable to actually have side effects

function fn(obj: { name: string | number }) {
  if (typeof obj.name === "string") {
    mut();
    // Does not error
    console.log(obj.name.toLowerCase());
  }

  function mut() {
    obj.name = 42;
  }
}
@Juan5212 Juan5212 added the Duplicate An existing issue was already created label Sep 30, 2024
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

1 participant