Skip to content

JSDoc optional property #39111

Closed
Closed
@lieser

Description

@lieser

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:

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions