Microsoft/TypeScript

Optional properties on function interface breaks inference of type parameters

Open

#52,262 建立於 2023年1月16日

在 GitHub 查看
 (2 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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.

貢獻者指南