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

Control flow analysis for dependent parameters only works when checking against literals #55766

Open
arcanis opened this issue Sep 17, 2023 · 2 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@arcanis
Copy link

arcanis commented Sep 17, 2023

πŸ”Ž Search Terms

narrow, parameters, typeof

πŸ•— Version & Regression Information

It never worked for typeof / asserts checks since the feature introduction in #47190; it's been mentioned here:

#47190 (comment)

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZmAMgSygUwE4EMA2AuGACgDpSsMBzCAgbQAMs6AaGMAVwFsAjTAXRgA+Mel2YxoGZGAq8AlDAC8APhgA3EMgAmiolhZd5ymAG8AUDBjI4uxQoUwARFgfyzFi6EggcaYjhAUhAYA3ObuAPTh7tHuAHoA-GEAvqYppp7Q8GAAghAQmFAEJGSU1MIMYuzcfILloiwSUjKGKupaOoR6MAaKKm6W1oTIENmdsq5hHuAQ3r7+gSGTMJExMQnJqenTsAgAKgCeAA5oIHBFpMTkVLRVPBgstzVCNI3SDVCS0nK9ahra9p19C0TGErEQoEcTtYsLZ7A5HhgXCDohlZn4AkFZKFoitVnFEhYUmk4GwwMAoMhwJYRoRVLgCKSANZgEAAdzAsgIdJw1JgFWRMAwaCgbAwYBgHzYaFCKSAA

πŸ’» Code

const fnLiteral: (...args: [`a`, number] | [`b`, string]) => void = (a, b) => {
  if (a === "a") {
    console.log(b);
    //          ^? number
  }
}

const fnAssert: (...args: [`a`, number] | [`b`, string]) => void = (a, b) => {
  if (isA(a)) {
    console.log(b);
    //          ^? number | string
  }
}
const fnTypeof: (...args: [number, number] | [string, string]) => void = (a, b) => {
  if (typeof a === "number") {
    console.log(b);
    //          ^? number | string
  }
}

function isA(val: unknown): val is `a` {
  return true;
}

πŸ™ Actual behavior

Both fnAssert and fnTypeof are unable to get TS to recognize b as its narrowed type (number). Only if a is checked against a literal does the narrowing works.

πŸ™‚ Expected behavior

The b parameter should be narrowed to number.

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Currently only discriminated unions are supported and it only works if you check against the discriminant. This isn't unique to dependent parameters - it works exactly in the same way with object types: TS playground

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Sep 18, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 18, 2023
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Sep 18, 2023
@danvk
Copy link
Contributor

danvk commented Jan 24, 2024

Dupe of #32399?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

4 participants