Microsoft/TypeScript

Empty rest parameter type inferred from candidate function with no rest parameters when "best match" contains a contextually typed argument

Open

#52,227 opened on Jan 13, 2023

View on GitHub
ย (4 comments)ย (0 reactions)ย (0 assignees)TypeScriptย (48,455 stars)ย (6,726 forks)batch import
Experience EnhancementHelp WantedSuggestion

Description

Bug Report

๐Ÿ”Ž Search Terms

Rest parameter inference contextual

๐Ÿ•— 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

const doSomething = <Args extends unknown[]>(
    fn1: (aNumber: number, ...rest: Args) => void,
    fn2: (aNumber: number, ...rest: Args) => void,
) => {}

// OK, no contextual types
doSomething(
    (explicit: number, rest: string) => {}, // Args = [string]
    () => {},
)

// OK (both contain contextually typed arg)
doSomething(
    (contextual, rest: string) => {}, // Args = [string]
    (contextual) => {},
)

// Contextually type only fn1 - Not ok
doSomething(
    (contextual, rest: string) => {}, // ERROR
    () => {}, // Args = []
)
doSomething(
    (contextual, rest: string) => {}, // ERROR
    (explicit: number) => {}, // Args = []
)

๐Ÿ™ Actual behavior

When fn1 (and only fn1) contains a contextually typed argument, it is ignored in favour of fn2

๐Ÿ™‚ Expected behavior

In all of the examples, fn2 specifies no rest parameters at all.

I would expect functions that do not specify any rest parameters to always be the least preferable candidate (lowest weight?) for rest parameter inference, since "no rest parameters" can always be assigned to any rest parameter type.

Contributor guide

Empty rest parameter type inferred from candidate function with no rest parameters when "best match" contains a contextually typed argument ยท Microsoft/TypeScript#52227 | Good First Issue