flowtype function overloading error: `Could not decide which case to select.`

Viewed 304

TS - passed

[{name: 1}] is matched with { value: T }[] and T[] the first signature is chosen, correct type is returned T[];

// TS example
function transformDataToForm<T>(data: { value: T }[]): T[];
function transformDataToForm<T>(data: T[]): T[];
function transformDataToForm<U, T extends { value?: U }>(data: T[]): (T | U)[] {
    return data.map(item => item.value ? item.value : item);
}

const res1: {name: number}[] = transformDataToForm([{name: 1}]);
const res2: number[] = transformDataToForm([{value: 1}]);                                    

Flow - error

I think for flow should exist only one signature to match.

declare function transformDataToForm<T>(data: { value: T }[]): T[];
declare function transformDataToForm<T>(data: T[]): T[];
function transformDataToForm<U, T: { value?: U }>(data: T[]): (T | U)[] {
    return data.map(item => item.value ? item.value : item);
}        

const res1: {name: number}[] = transformDataToForm([{name: 1}]);
//                             ^ Could not decide which case to select. Since case 1 [1] may work but if it doesn't case 2 [2] looks promising too. To fix add a type annotation to inferred union of array element types (alternatively, provide an annotation to summarize the array element type) [3]      

// const res2: number[] = transformDataToForm([{value: 1}]);                                    

Try flow

What is the correct way to use function overloading in Flow?

What are the correct types for transformDataToForm?

0 Answers
Related