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

TS2345: Fails when both branches of a conditional generic are valid #41590

Closed
ArnaudBarre opened this issue Nov 19, 2020 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@ArnaudBarre
Copy link

TypeScript Version: 4.0.5, 4.1-beta, 4.2.0-dev

Search Terms: TS2345 conditional generic

Code: (Playground Link)

export const DatePicker = <IsClearable extends boolean>({
  value, onChange, isClearable
}: {
  value: string | undefined
  onChange: (
    value: IsClearable extends true ? string | undefined : string,
  ) => void;
  isClearable?: IsClearable;
}) => (
  <>
    <div>{value ?? "Choose"}</div>
    // In an ideal world this one would works, but that's not the biggest issue
    {value && isClearable && <button onClick={() => onChange(undefined!)}>Clear</button>}
    // This one seems more like a bug
    <input type="date" onChange={e => onChange(e.target.value)} />
  </>
);

Expected behavior: No error

Actual behavior: Argument of type 'string' is not assignable to parameter of type 'IsClearable extends true ? string | undefined : string'

@andrewbranch
Copy link
Member

Duplicate of #26933

@andrewbranch andrewbranch marked this as a duplicate of #26933 Nov 24, 2020
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 24, 2020
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

2 participants