简体   繁体   English

JSDoc Typescript 检查命名默认参数

[英]JSDoc Typescript Checking of Named Default Params

Given the following鉴于以下

/**
 * @param {{bar?: string|null}} [param0]
 */
const foo = ({bar = null} = {}) => bar;

Typescript 3.7.2 reports Typescript 3.7.2 报告

var bar: string | null Binding element 'bar' implicitly has an 'any' type.ts(7031)

The JavaScript code works how I want it to, but how can I write the jsdoc hint so that TypeScript understands that the destructured bar variable is string|null not any ? JavaScript 代码按我希望的方式工作,但是我如何编写 jsdoc 提示,以便 TypeScript 理解解构的bar变量是string|null而不是any

Update with correct answer更新正确答案

The problem found is actually your tsconfig and how the JavaScript type checking works with it.发现的问题实际上是您的 tsconfig 以及 JavaScript 类型检查如何使用它。 The option that is causing the difference between your config and mine is the strict property.导致您的配置和我的配置之间存在差异的选项是strict属性。 According to the config documentation :根据配置文档

Enabling --strict enables --noImplicitAny , --noImplicitThis , --alwaysStrict , --strictBindCallApply , --strictNullChecks , --strictFunctionTypes and --strictPropertyInitialization .启用--strict启用--noImplicitAny--noImplicitThis--alwaysStrict--strictBindCallApply--strictNullChecks--strictFunctionTypes--strictPropertyInitialization

Adding each of those options to my tsconfig , I discovered that disabling two of those options would get rid of the reported error.将这些选项中的每一个添加到我的tsconfig ,我发现禁用其中两个选项将消除报告的错误。 Those options are:这些选项是:

  • --strictNullChecks
  • --strictPropertyInitialization (because it can't be enabled without the --strictNullChecks option) --strictPropertyInitialization (因为它不能在没有--strictNullChecks选项的情况下启用)

When I disabled those two, it was finally happy with the result.当我禁用这两个时,它终于对结果感到满意。

I copied the example into a TypeScript file to see if it had a similar problem.我将示例复制到一个 TypeScript 文件中,看看它是否有类似的问题。 The TypeScript version reported no errors with the same exact type signature, but the JavaScript version was ignoring the JSDoc altogether. TypeScript 版本没有报告具有完全相同的类型签名的错误,但 JavaScript 版本完全忽略了 JSDoc。 However, looking at the rest of the code, it still was registering the bar variable as being of type string|null .但是,查看其余代码,它仍然将bar变量注册为string|null类型。

Coming to this conclusion, it is likely a bug in the JavaScript type checking and how it works with those options despite neither of those options seemingly being related to this case.得出这个结论,这可能是 JavaScript 类型检查中的一个错误,以及它如何与这些选项一起工作,尽管这些选项似乎都与这种情况无关。

EDIT:编辑:

I've checked and it looks like there is a bug already logged for this on the TypeScript repo:我已经检查过,在 TypeScript 存储库上似乎已经为此记录了一个错误:

https://github.com/microsoft/TypeScript/issues/31372 https://github.com/microsoft/TypeScript/issues/31372

Previous solution (which didn't work)以前的解决方案(不起作用)

You should be able to do this easily.您应该能够轻松做到这一点。 Maybe remove the square brackets from the param name in the jsdoc comment.也许从 jsdoc 注释中的参数名称中删除方括号。 This example works fine:这个例子工作正常:

/**
 * @param {{bar?: number|null}} _
 */
const foo = ({ bar = null } = {}) => {
    // do stuff with bar
}

The tsconfig that I have looks like this:我拥有的 tsconfig 如下所示:

{
    "compilerOptions": {
        "checkJs": true,
        "allowJs": true,
        "outDir": "node_modules/.tmp/",
        "noImplicitAny": true
    },
    "include": [
        "index.js"
    ]
}

Here is the repo that has this code you can use to check on your side, too: https://github.com/cwadrupldijjit/js-typescript-checking这是包含此代码的存储库,您也可以使用它进行检查: https : //github.com/cwadrupldijjit/js-typescript-checking

The version of TypeScript that this was tested in is 3.7.2测试的 TypeScript 版本是 3.7.2

EDIT: Checked out the square brackets vs non-square brackets and it looks like it doesn't matter.编辑:检查方括号与非方括号,看起来无关紧要。 TSC is fine with it either way.无论哪种方式,TSC 都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM