<!DOCTYPE qhelp PUBLIC"-//Semmle//qhelp//EN""qhelp.dtd"><qhelp><overview><p>If the same pattern variable is bound multiple times in the same object or array pattern, the lastbinding overwrites all of the earlier ones. This is most likely unintended and should be avoided.</p><p>In TypeScript, a common mistake is to try to write type annotations inside a pattern. This is notpossible, and the type annotation should come after the pattern.</p></overview><recommendation><p>Rename the pattern variables to have different names. In an array pattern, elements that do not needto be bound can be omitted.</p></recommendation><example><p>In the following example, the function <code>distanceFromOrigin</code> uses an array pattern to decomposeits argument <code>point</code>. The pattern binds <code>x</code> twice: first, <code>x</code> isbound to <code>point[0]</code>, the first element of <code>point</code>; this binding is then immediatelyoverwritten by a binding to <code>point[1]</code>, which is probably unintended.</p><sample src="examples/NonLinearPattern.js" /><p>From context, it appears that the second binding should have been for variable <code>y</code> like this:</p><sample src="examples/NonLinearPatternGood.js" /><p>This can sometimes happen in TypeScript, due to the apparent similarity between property patternsand type annotations. In the following example, the function uses a pattern parameter with properties <code>x</code>and <code>y</code>. These appear to have type <code>number</code>, but are in fact untyped properties both stored in a variable named <code>number</code>.</p><sample src="examples/NonLinearPatternTS.ts" /><p>It is not possible to specify type annotations inside a pattern. The correct way is to specify the typeafter the parameter:</p><sample src="examples/NonLinearPatternTSGood.ts" /></example><references><li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">Destructuring assignment</a>.</li></references></qhelp>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。