typescript - 在 TypeScript 中 GeneratorFunction 接口代表什么?
问题描述
function* 声明(function 关键字后跟一个星号)定义了一个生成器函数,它返回一个 Generator 对象。[参考]
但如果我这样做:
const genFn: GeneratorFunction = function* () {};
我得到:
Property '[Symbol.toStringTag]' is missing in type '() => Generator<never, void, unknown>' but required in type 'GeneratorFunction'.ts(2741)
lib.es2015.symbol.wellknown.d.ts(161, 14): '[Symbol.toStringTag]' is declared here.
在 TypeScript 中 GeneratorFunction 接口代表什么?另外,是否有更合适的内置接口来捕获生成器功能?
解决方案
以下是一般的四个级别的注释。所有这些都将编译没有错误:
// Truly general
const genFnA: Function = function* () {};
// Bit less general
const genFnB: () => Generator = function* () {};
// Exact manual
const genFnC: () => Generator<never, void, unknown> = function* () {};
// Best: Use the inference
const genFnD = function* () {};
推荐阅读
- c++ - 数组声明错误:“变量'...'周围的堆栈已损坏”
- amazon-web-services - 如何允许 cognito 身份验证的用户获得对 s3 存储桶的公共访问权限
- flutter - FlutterViewController setInitialRoute for windows
- javascript - 单击时如何更改下拉按钮的插入符号?HTML CSS Javascript
- android - 如何使来自 SQLite 的列表视图中的项目可被 kotlin 点击?
- r - “x1[, 2] * pi 中的错误:二元运算符的非数字参数”在薄 () 操作中
- django - 带有 PostgreSQL 的 Dockerized Django APP 不起作用
- flutter - “图像”类型的值不能分配给“图像”类型的变量?
- c# - 无法在 ObjectViewList TreeView 中显示文本
- python - 使用图片制作 wordpress 帖子