TypeScript JSDoc support for @yields

fbcarpbf  于 9个月前  发布在  TypeScript
关注(0)|答案(6)|浏览(136)

使用 @yields JSDoc 标签会很好。
我们与JS和JSDoc一起工作,但利用TSC和VSCode。
我们还使用 valid-jsdoc eslint规则。

我们有:

  1. /** @typedef Thing (...) */
  2. function* walkThings() {
  3. //... some yield here and there
  4. }

我们想要的:

  1. /**
  2. * @yields {Thing}
  3. */
  4. function* walkThings() {
  5. //... some yield here and there
  6. }

我们不能做的:

  1. /**
  2. * @returns {IterableIterator<Something>}
  3. */
  4. function* walkThings() {
  5. //... some yield here and there
  6. }

因为它不支持 return ,而是支持 yield ,而且eslint对此发出了抱怨。我们可以在这里禁用该规则,但这不是理想的情况。

mkshixfv

mkshixfv1#

在这里补充一下,听起来像是ESLint那边的bug。

uujelgoq

uujelgoq2#

在这里插一句,听起来这也是ESLint的问题。
我对@DanielRosenwasser的说法不太有信心:

  • returnyield 在句法层面上具有不同的含义。
  • IterableIterator<T> 是实现细节,代码没有返回可迭代的迭代器,也不是开发者编写的。只是多个 yield
  • 我找不到任何关于TypeScript定义为 IterableIterator 的标准文档,所以我不指望eslint能知道它。只是好奇:它从哪里来?是TypeScript生成器的内部定义吗?
liwlm1x9

liwlm1x93#

这是TypeScript生成器的内部定义吗?
它是一个接口(嗯,suite of them),定义了生成器或IterableIterator规范对象的形状:

  1. interface IteratorResult<T> {
  2. done: boolean;
  3. value: T;
  4. }
  5. interface Iterator<T> {
  6. next(value?: any): IteratorResult<T>;
  7. return?(value?: any): IteratorResult<T>;
  8. throw?(e?: any): IteratorResult<T>;
  9. }
  10. interface Iterable<T> {
  11. [Symbol.iterator](): Iterator<T>;
  12. }
  13. interface IterableIterator<T> extends Iterator<T> {
  14. [Symbol.iterator](): IterableIterator<T>;
  15. }
展开查看全部
pw9qyyiw

pw9qyyiw4#

感谢@weswigham的回复。我已经注意到了(这就是为什么我在JSDoc部分使用它的原因)。我想了解是否有类似HTMLElement的标准定义方式,或者这是否是TypeScript开发者为了模拟可迭代迭代器示例而制定的定义(考虑到PromiseLike<T>)。
无论如何,你解决了我的问题,再次感谢!

r7xajy2e

r7xajy2e5#

原始评论中的 eslint 规则现已弃用,但 https://github.com/gajus/eslint-plugin-jsdoc 可以与 @yield 一起满足(至少在推荐的规则集中)。Typescript 需要 @return。两者都可以使用,但这并不理想:

  1. /**
  2. * @template {boolean} [E=null]
  3. * @template {boolean} [A=null]
  4. * @template {boolean} [T=null]
  5. * @template {boolean} [C=null]
  6. * @param {Node} root Root node to walk
  7. * @param {Object} filter Tree walking options
  8. * @param {E} [filter.element] Yield elements
  9. * @param {A} [filter.attribute] Yield attributes
  10. * @param {T} [filter.text] Yield Text node
  11. * @param {C} [filter.comment] Yield Comment nodes
  12. * @yields {
  13. * (E extends null ? null : Element) |
  14. * (A extends null ? null : Attr) |
  15. * (T extends null ? null : Text) |
  16. * (C extends null ? null : Comment)
  17. * }
  18. * @return {Iterable<
  19. * (E extends null ? null : Element) |
  20. * (A extends null ? null : Attr) |
  21. * (T extends null ? null : Text) |
  22. * (C extends null ? null : Comment)
  23. * >} Iterable
  24. */
  25. export function* iterateNodes(root, filter = {}) {

我在JSDoc中使用了TS语法,所以我禁用了 "jsdoc/valid-types"。除此之外,eslint是满意的。
编辑:Intellisense(TS)也将其解析得很好。

工具提示文档错误已在此提交:microsoft/vscode#164888
编辑2:我认为我应该使用 Generator<T> 而不是 Iterator<T>

展开查看全部
ekqde3dh

ekqde3dh6#

我认为我应该使用 Generator<T> 而不是 Iterator<T>
看起来是这样。在我的设备上就是这样的。

  1. /** Runs `forEach()` callback over warehouses cache
  2. *
  3. * @returns {Generator<EqShowItem>}
  4. */
  5. export function* forEachWarehouse() {
  6. // …
  7. }

然后 [...module.forEachWarehouse()].filter((wh) => …) 可以正确推断类型。

相关问题