TypeScript/tests/cases/conformance/jsdoc/paramTagBracketsAddOptionalUndefined.ts
Nathan Shively-Sanders 0fa838a3ef
Brackets and postfix= in @param add undefined (#22514)
* Brackets and postfix= in `@param` add undefined

Previously they only added optionality.
Note that, unlike Typescript, when a parameter initializer is specified
in jsdoc, it does not remove undefined in the *body* of the function.
That's because TS will generate initialisation code, but JS won't, so
the author will have to manually write code to remove undefined from the
type.

```js
/** @param {number} [a=101] */
function f(a) {
  // a: number | undefined here
  if (!a) {
    a = 101
  }
  // a: number here
}
```

Note that we don't check that
1. the initializer value is actually assigned to the parameter.
2. the initializer's type matches the declared type of the parameter.

Pretty much we just parse it and leave it alone.

* Address PR comments
2018-03-13 15:56:38 -07:00

22 lines
418 B
TypeScript

// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js
/**
* @param {number} [p]
* @param {number=} q
* @param {number} [r=101]
*/
function f(p, q, r) {
p = undefined
q = undefined
// note that, unlike TS, JSDOC [r=101] retains | undefined because
// there's no code emitted to get rid of it.
r = undefined
}
f()
f(undefined, undefined, undefined)
f(1, 2, 3)