Skip to content

CFA doesn't work when using generics to associate two function arguments #50205

Description

@k8w

Bug Report

🔎 Search Terms

CFA generic function

🕗 Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

💻 Code

interface RouteParams {
  '/user': { userId: string },
  '/product': { productId: string },
  '/list': { pageSize: number, current: number },
}

function navigateTo<T extends keyof RouteParams>(url: T, params: RouteParams[T]) {
  // NOT WORK HERE !!!
  if (url === '/user') {
    console.log(params.userId)
  }
}

// WORKS WELL
navigateTo('/user', { userId: '123' });
// @ts-expect-error
navigateTo('/user', { xxxxx: '123' });

🙁 Actual behavior

Property 'userId' does not exist on type '{ userId: string; } | { productId: string; } | { pageSize: number; current: number; }'.
Property 'userId' does not exist on type '{ productId: string; }'.(2339)

CFA works well outside the function call, but not works inside the function.

🙂 Expected behavior

Compile successfully.

Activity

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