Skip to content

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

@ArnaudBarre

Description

@ArnaudBarre

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions