Skip to content

Cannot infer const array when put as function args since version 4.2Β #44631

Closed

Description

Bug Report

πŸ”Ž Search Terms

Generics, const assertions, Tuple

πŸ•— Version & Regression Information

  • This changed between versions v4.1.5 and v4.2.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type State = {
  foo: string
}
// Selector is a function which has zero or some args.
type InputSelector<T, P = void> = (
  state: State,
  props: P,
  ...args: any[]
) => T;
export function createInputSelector<T, P = void>(selector: InputSelector<T, P>): InputSelector<T, P> {
  return selector;
}

// In this sample, return tuple array as is. 
export function createSelectors<
  T,
  R extends readonly InputSelector<T, any>[],
>(selectors: R): R {
  return selectors
}

// Case 1: Use defined const array
// -> All version: readonly [InputSelector<string, void>]
const constArr = [createInputSelector((state: State) => state.foo)] as const
const res1 = createSelectors(constArr)
// Case 2: Define const array in function args
// -> v4.2.2~: readonly [InputSelector<string, any>]
//    ~v4.1.5: readonly [InputSelector<string, void>]
const res2 = createSelectors([createInputSelector((state: State) => state.foo)] as const)

πŸ™ Actual behavior

Cannot infer tuple type since v4.2 (return type becomes extension source)

πŸ™‚ Expected behavior

Infer tuple type even when define const array in function args

Activity

  1. RyanCavanaugh commented on Jun 17, 2021

    @RyanCavanaugh
    Member

    This is the intended behavior as of 4.2; a generic default is only applied in the presence of zero inference candidates, but createSelectors applies a contextual inference site from its constraint, thus supplying a candidate for P.

  2. dora1998 commented on Jun 18, 2021

    @dora1998
    Author

    Thank you for the quick reply.
    I undestand inference context, but why did this behavior change in 4.2?

    I cannot find the relevant PRs or issues, so if you can, could you tell me those?

  3. typescript-bot commented on Jun 20, 2021

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions