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

Conditional type not narrowed by control flow #45766

Closed
PaperStrike opened this issue Sep 7, 2021 · 1 comment
Closed

Conditional type not narrowed by control flow #45766

PaperStrike opened this issue Sep 7, 2021 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@PaperStrike
Copy link

Bug Report

πŸ”Ž Search Terms

conditional guard, conditional flow

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type PackageForFunctions<T> = T extends Function ? (required: true) => T : T;

const getOriginal = <T>(value: PackageForFunctions<T>): T => {
  if (typeof value === 'function') return value(false);
  return value;
};

πŸ™ Actual behavior

  • No error on value(false). Wrong for value can only be a function when it is (required: true) => T.
  • Error "not assignable to type 'T'" in the last return. Wrong for value must be T if it is not a function.

πŸ™‚ Expected behavior

  • Throw an error as the required param is true on value(false).
  • No error in the last return as it must be T at that line.
@jcalz
Copy link
Contributor

jcalz commented Sep 7, 2021

Duplicate of #33912

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 7, 2021
hyrumcoop added a commit to hyrumcoop/TypeScript that referenced this issue Sep 9, 2021
hyrumcoop added a commit to hyrumcoop/TypeScript that referenced this issue Sep 9, 2021
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