Closed
Description
The JSDoc optional property syntax described on https://jsdoc.app/tags-property.html behaves unexpectedly.
TypeScript Version: 3.9.5, Version 4.0.0-dev.20200615
Search Terms: jsdoc, optional
Code
/**
* User type definition
* @typedef {Object} User
* @property {string} email
* @property {string} [nickName]
*/
/** @type {User} */
const user = {email: ""}; // as expected email is required, but nickName is not
const nickName = user.nickName; // expected type string|undefined, but type is string
// expected error TS2532: Object is possibly 'undefined'.
console.log(nickName.length)
Expected behavior:
nickName
has type string|undefined
, and the console.log
line should be an error with strict
enabled
Actual behavior:
nickName
has type string
, no error reported even with strict
enabled.
Playground Link:
Related Issues: