简体   繁体   English

在typescript中的type参数中使用的never关键字

[英]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.

相关问题 打字稿:&#39;{translationSentences:从不[]; }&#39; 不可分配给类型为 &#39;never&#39; 的参数 - Typescript: '{ translatedSentences: never[]; }' is not assignable to parameter of type 'never' Typescript never[] 类型的参数不可分配给 SetStateAction 类型的参数<never[]></never[]> - Typescript Argument of type never[] is not assignable to parameter of type SetStateAction<never[]> React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' 如果参数类型为“never”,typescript ReturnType 为“any” - typescript ReturnType is “any” if parameter type is “never” TypeScript 中的“不能分配给从不类型的参数”错误是什么? - What is "not assignable to parameter of type never" error in TypeScript? Typescript:“无法分配给永不类型的参数”错误 - Typescript: “not assignable to parameter of type never” error 在打字稿中使用 never 关键字 - Use of never keyword in typescript “文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应 - Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript “any”类型的参数不可分配给“never”类型的参数 typescript 解决方案 - argument of type 'any' is not assignable to parameter of type 'never' typescript solution 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM