Closed
Description
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.