Typescript: Same generic type as resolved type of previous parameter

后端 未结 1 1622
北海茫月
北海茫月 2021-01-13 06:26

I would like to know, how to specify that generic type if same as the one the resolved type of previous argument, when the type can be of multiple types.

TypeScript

1条回答
  •  醉梦人生
    2021-01-13 06:42

    You can't narrow the type of the generic parameter within the function. So when you test a this will not tell the compiler what the type of b is. And more importantly it will not tell the compiler what the return type of the function needs to be

    function add(a: T, b: T): T {
        if (typeof a === 'string' && typeof b === 'string') {
            let result = a + b; // result is string, we can apply + 
            return result as T; // still an error without the assertion, string is not T 
        } else if (typeof a === 'number' && typeof b === 'number') {
            let result = a + b; // result is number, we can apply +
            return result as T; // still an error without the assertion, number is not T  
        }
        throw "Unsupported parameter type combination"; // default case should not be reached
    }
    

    In this case though maybe having a dedicated implementation signature that works on the union instead (meaning no assertion is required) and public signature being the one you previously used.:

    function add(a: T, b: T): T
    function add(a: number | string, b: number | string): number | string {
        if (typeof a === 'string' && typeof b === 'string') {
            return a + b;
        } else if (typeof a === 'number' && typeof b === 'number') {
            return a + b;
        }
        throw "Unsupported parameter type combination"; // default case should not be reached
    }
    

    0 讨论(0)
提交回复
热议问题