TypeScript 在使用--allowJs和--declaration选项时,箭头函数缺少JSDoc描述,

mbzjlibv  于 4个月前  发布在  TypeScript
关注(0)|答案(4)|浏览(61)

我正在尝试在一个仅包含JavaScript的项目中使用--allowJs --declaration,并注意到箭头函数的JSDoc描述没有正确地附加到类型定义上。

TypeScript版本: 3.8.0-dev.20191031
搜索词: 箭头函数, allowJs, 声明
代码

https://github.com/hipstersmoothie/typescript-jsdoc-bug

/**
* This isn't included
*
* @param bar a test param
*/
export const foo = bar => {};

/**
* This is included
*
* @param baz a test param
*/
export function fob(baz) {}

预期行为:(至少像这样)

/**
* This isn't included
*
* @param bar a test param
*/
export function foo(bar: any): void;
/**
* This is included
*
* @param baz a test param
*/
export function fob(baz: any): void;

实际行为:

/**
* This is included
*
* @param baz a test param
*/
export function fob(baz: any): void;
export function foo(bar: any): void;

如果我将文件切换到.ts,输出将是预期的

/**
* This isn't included
*
* @param bar a test param
*/
export declare const foo: (bar: string) => void;
/**
* This is included
*
* @param baz a test param
*/
export declare function fob(baz: string): void;
2vuwiymt

2vuwiymt1#

翻译结果为:相关地,这里有一个基线测试,其中所有导出的成员都应该有JSDoc。我发现这个问题也发生在你在$x_1m_0n_1^x$上直接定义导出时

$x_1a_0b_1^x \rightarrow x_1a_1b_1^x$

mfuanj7w

mfuanj7w2#

我已经遇到了与@hipstersmoothie相同的问题,并找到了两个可能目前有用的解决方法:

JavaScript代码
export const foo =
  /**
* This is included
*
* @param bar a test param
*/
  bar => {};

/**
* This is included
*
* @type {(bar: any) => void}
*/
export const foo2 = bar => {};
声明输出
/**
* This is included
*
* @param bar a test param
*/
export function foo(bar: any): void;
/**
* This is included
*
* @type {(bar: any) => void}
*/
export const foo2: (bar: any) => void;
ahy6op9u

ahy6op9u3#

这是四年前为3.8.1安排的。@RyanCavanaugh仍然认为值得修复吗?
https://github.com/Endojs/endo/中,模块位于.js,我们发出.d.ts用于NPM。代码大量使用了箭头函数,因此由于这个bug,许多JSDoc从.d.ts中丢失了。

1tu0hz3e

1tu0hz3e4#

仍然没有解决方案吗?我仍然在使用*.ts文件时遇到相同的问题:
jsdoc.json:

{
  "plugins": [
    "plugins/markdown",
    "jsdoc-escape-at",
    "jsdoc-ts-utils",
    "better-docs/typescript"
  ],
  "typescript": {
    "module": "ESNext",
    "target": "ES2022"
  },
  "source": {
    "include": [
      "src"
    ],
    "includePattern": "^.+\\.ts(x)?$"
  },
  "sourceType": "module",
  "opts": {
    "destination": "doc",
    "excludeExternals": true,
    "readme": "README.md",
    "package": "package.json"
  }
}

示例文档:

/**
* Sorts an array of objects by specified keys.
* @param {string} key - The primary key to sort by.
* @param {...string} keys - Additional keys to sort by.
* @returns {Function} A comparator function.
* @example
* const data = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 1}]
* data.sort(sortByKey('a', 'b')) // [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}]
*/
export const sortByKey = (key: string, ...keys) => (a: object, b: object) =>
  a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : keys[0] ? sortByKey(keys[0], keys.slice(1))(a, b) : 0

生成的输出:

(constant) sortByKey

Sorts an array of objects by specified keys.

Source: array.ts, line 11

Example

const data = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 1}]
data.sort(sortByKey('a', 'b')) // [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}]

因此无法识别为函数,参数和返回值未记录。
对我来说,这也不起作用:

/**
* Sorts an array of objects by specified keys.
* @example
* const data = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 1}]
* data.sort(sortByKey('a', 'b')) // [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}]
*/
export const sortByKey =
  /**
*  @param {string} key - The primary key to sort by.
* @param {...string} keys - Additional keys to sort by.
* @returns {Function} A comparator function.
*/
  (key: string, ...keys) => (a: object, b: object) =>
    a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : keys[0] ? sortByKey(keys[0], keys.slice(1))(a, b) : 0

这个:

/**
* Sorts an array of objects by specified keys.
* @param {string} key - The primary key to sort by.
* @param {...string} keys - Additional keys to sort by.
* @returns {Function} A comparator function.
* @example
* const data = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 1}]
* data.sort(sortByKey('a', 'b')) // [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}]
*/
export function sortByKey(key: string, ...keys) {
  return function (a: object, b: object) {
    return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : keys[0] ? sortByKey(keys[0], keys.slice(1))(a, b) : 0
  }
}

最后产生的是:

sortByKey(key, …keys) → {function}

Sorts an array of objects by specified keys.
Parameters:
Name 	Type 	Attributes 	Description
key 	string 		

The primary key to sort by.
keys 	string 	<repeatable>
	

Additional keys to sort by.

Source:

        array.ts, line 11 

Returns:

A comparator function.

Type
    function 

Example

const data = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 1}]
data.sort(sortByKey('a', 'b')) // [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}]

但我喜欢箭头语法,想使用那个...!

相关问题