Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional arguments are not marked as optional (JSDoc) #1804

Closed
3bl3gamer opened this issue Nov 25, 2021 · 0 comments
Closed

Optional arguments are not marked as optional (JSDoc) #1804

3bl3gamer opened this issue Nov 25, 2021 · 0 comments
Labels
bug Functionality does not match expectation
Milestone

Comments

@3bl3gamer
Copy link

3bl3gamer commented Nov 25, 2021

Search terms

optional, isOptional, argument, parameter, flags, JSDoc

Expected Behavior

Expected isOptional flag to be set for arguments, marked as optional with JSDoc (@param {type} [arg]).

Actual Behavior

Argument is required, but it's type gets union'ed with undefined.

Steps to reproduce the bug

// index.js
/**
 * @param {number} [arg]
 */
export function foo(arg) {}
// tsconfig.json
{
	"compilerOptions": {
		"allowJs": true,
		"checkJs": true,
		"strict": true,
		"declaration": true,
		"declarationDir": "./types",
		"emitDeclarationOnly": true
	},
	"exclude": ["node_modules"]
}
./node_modules/.bin/typedoc index.js --json docs.json
cat docs.json

Output:

Info: JSON written to ./docs.json
{
	"id": 0,
	"name": "undefined",
	"kind": 1,
	"kindString": "Project",
	"flags": {},
	"originalName": "",
	"children": [
		{
			"id": 1,
			"name": "foo",
			"kind": 64,
			"kindString": "Function",
			"flags": {},
			"sources": [
				{
					"fileName": "index.js",
					"line": 4,
					"character": 16
				}
			],
			"signatures": [
				{
					"id": 2,
					"name": "foo",
					"kind": 4096,
					"kindString": "Call signature",
					"flags": {},
					"comment": {},
					"parameters": [
						{
							"id": 3,
							"name": "arg",
							"kind": 32768,
							"kindString": "Parameter",
							"flags": {},
							"type": {
								"type": "union",
								"types": [
									{
										"type": "intrinsic",
										"name": "undefined"
									},
									{
										"type": "intrinsic",
										"name": "number"
									}
								]
							}
						}
					],
					"type": {
						"type": "intrinsic",
						"name": "void"
					}
				}
			]
		}
	],
	"groups": [
		{
			"title": "Functions",
			"kind": 64,
			"children": [
				1
			]
		}
	],
	"sources": [
		{
			"fileName": "index.js",
			"line": 4,
			"character": 0
		}
	]
}

Works correctly when used with declarations generated by tsc:

./node_modules/.bin/tsc --project tsconfig.json
./node_modules/.bin/typedoc types/index.d.ts --json docs.json
cat docs.json
"parameters": [
    {
        "id": 3,
        "name": "arg",
        "kind": 32768,
        "kindString": "Parameter",
        "flags": {
            "isOptional": true
        },
        "type": {
            "type": "intrinsic",
            "name": "number"
        }
    }
],
// generated types/index.d.ts
/**
 * @param {number} [arg]
 */
export function foo(arg?: number | undefined): void;

Environment

  • Typedoc version: v0.22.10
  • TypeScript version: v4.5.2
  • Node.js version: v16.11.1
  • OS: Arch Linux
@3bl3gamer 3bl3gamer added the bug Functionality does not match expectation label Nov 25, 2021
@Gerrit0 Gerrit0 added this to To do in TSDoc - v0.23 via automation Nov 25, 2021
@Gerrit0 Gerrit0 moved this from To do to Done in TSDoc - v0.23 Feb 18, 2022
@Gerrit0 Gerrit0 added this to the v0.23 milestone Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Functionality does not match expectation
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants