[英]never keyword used in type parameter in typescript
I am trying to understand the following code: 我试图理解以下代码:
export const getArraySemigroup = <A = never>(): Semigroup<Array<A>> => {
return {
concat: (x, y) => concat(x, y)
}
}
My understanding of never
is that it is something that will never be resolved, is it being used as a default paramter her? 我的理解
never
是它的东西,永远不会得到解决,它是被用来作为默认放慢参数她吗?
I've never (pun intended) seen a type parameter with an equals sign before. 我以前从未(双关语意)见过带有等号的类型参数。
<A = never>
is generic type default value . <A = never>
是泛型类型的默认值 。 By default, generic type default value is empty object, <{}>
. 默认情况下,泛型类型的默认值是空对象
<{}>
。
As stated in documentation , 如文档中所述 ,
The never type represents the type of values that never occur.
从不类型表示永不出现的值的类型。 For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns;
例如,永远不会抛出异常的函数表达式或箭头函数表达式的返回类型,或者永远不会返回异常的箭头函数表达式; Variables also acquire the type never when narrowed by any type guards that can never be true.
变量也永远不会被任何永远不可能为真的类型防护所缩小,从而获得类型。
This piece of code basically forces a developer to always use generic function with explicit type argument. 这段代码基本上迫使开发人员始终将泛型函数与显式类型参数一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.