phuocng / 1loc

What's your favorite JavaScript single LOC (line of code)?

Home Page:https://phuoc.ng/collection/1-loc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

what is the diffrence between <T> and <T, _>

luckydog12 opened this issue · comments

i can't understand

Hi @luckydog12, you can identify an example so your question can be answered precisely !

const clone = <T, _>(arr: T[]): T[] => [...arr]
i try to ignore _ , use < T > like this: const clone = < T > (arr: T[]):T[] => [...arr]
it also work, so i don't know _ for what?????
can u understand what i mean ?

@luckydog12 <T, _> 相当于是2个参数类型的泛型约束,就像 <T, U> 写法一样,只是这里第二个参数类型约束没用上,因为你的示例代码只有一个参数,如果把你的示例改改,可能就好理解了。
比如:const clone = <T, _>(arr:T[], arr2:_[]):(T|_)[] => [...arr, ...arr2]
等同于:const clone = <T, U>(arr:T[], arr2:U[]):(T|U)[] => [...arr, ...arr2]