我熟悉语法
<T, U>
及其用途,但我对语法<T, U = T>
有点困惑,我在打字稿文档中找不到它。请推荐阅读材料。谢谢。
我尝试阅读打字稿文档,但我似乎找不到任何提及
<T, U = T>
它为
U
类型提供默认值,在本例中为 T
类型。
看一下这个例子:
// This is our good old generic type. It needs to be provided or it will be inferred if possible
function myFunc<T>() {}
// Same as before but, it will use `string` type for `T` unless it is provided
function myFunc<T = string>() {}
// Same as the first example with 2 type parameters
function myFunc<T, U>() {}
// Also 2 type parameters but the second one will be `string` if not provided
function myFunc<T, U = string>() {}
// Also 2 type parameters but the second one will be `T` if not provided
function myFunc<T, U = T>() {}