I'm attempting to develop a function with a generic type that takes one parameter with multiple types.
It seems straightforward, but when I combine certain types, I encounter compile-time issues in Visual Studio Code.
Check out my example below:
This code snippet works fine:
class Test<T>{
GetValue(value: string|boolean|number|undefined) {
return value;
}
}
new Test<Item>().getValue(4)
new Test<Item>().getValue(true)
This second piece of code also functions correctly:
class Test<T>{
GetValue<B>(value: (x: T) => B) {
return value;
}
}
new Test<Item>().getValue(x=> x.name)
However, the following code does not work. Any thoughts on why?
class Test<T>{
GetValue<B>(value: (x: T) => B|string|boolean|number|undefined) {
return value;
}
}
// this works
new Test<Item>().getValue(x=> x.name)
// this doesn't work - any ideas why?
new Test<Item>().getValue(true)