为什么具有多种类型的泛型在可视代码中触发编译时错误

Why Generic with multiple type trigger compile-time error in visual code

我正在尝试创建一个带有多个参数的通用类型函数。

这应该很容易,但是当我将一些类型混合在一起时,我得到 compile-time 的可视代码。

看下面我的例子

这项工作..

class Test<T>{
    GetValue(value: string|boolean|number|undefined) {
        return value;
    }
}

new Test<Item>().getValue(4)
new Test<Item>().getValue(true)

这也有效

    class Test<T>{
        GetValue<B>(value: (x: T) => B) {
            return value;
        }
    }

    new Test<Item>().getValue(x=> x.name)

但是这个不行。为什么?

    class Test<T>{
        GetValue<B>(value: (x: T) => B|string|boolean|number|undefined) {
            return value;
        }
    }
    // this work
    new Test<Item>().getValue(x=> x.name)
    
    // this do not work Why is that?
    new Test<Item>().getValue(true)

运算符优先级问题:

(x: T) => B|string|boolean|number|undefined

应该是:

((x: T) => B)|string|boolean|number|undefined

否则参数value被认为是returnsB|string|boolean|number|undefined

的函数

playground