Microsoft/TypeScript

Optional properties on function interface breaks inference of type parameters

Open

#52,262 opened on Jan 16, 2023

View on GitHub
 (2 comments) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Description

Bug Report

🔎 Search Terms

function interface optional properties generics type parameters inference

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface FuncA<T> {
  (arg: T): void;
};

interface FuncB<T> {
  (arg: T): void;
  x?: string;
};

const outerA = <T,>(func: FuncA<T>, arg: T) => {};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};

outerA(inner, (arg: 'a' | 'b') => {});
outerB(inner, (arg: 'a' | 'b') => {}); // error

🙁 Actual behavior

In the call to outerA(), the type of the func parameter is inferred as FuncA<(arg: 'a' | 'b') => void>, as determined by the type of the arg parameter.

In the call to outerB(), the type of the func parameter is inferred instead as FuncB<(arg: 'a' | 'b' | 'c') => void>, using the base constraint of inner's T parameter rather than the subtype of that constraint used by the arg parameter.

🙂 Expected behavior

I do not expect the addition of an optional property on FuncB<T> to break the type parameter inference. It seems that inner's T parameter should be inferred as 'a' | 'b' in both cases.

Contributor guide