Skip to content

Commit 37536e5

Browse files
committed
Use ?: instead of |undefined in .d.ts files
1 parent f8b415a commit 37536e5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/tsd-jsdoc/publish.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ function writeInterface(element) {
314314

315315
// handles a single element of any understood type
316316
function handleElement(element, parent, insideClass) {
317+
if (element.optional !== true && element.type && element.type.names && element.type.names.length) {
318+
for (let i = 0; i < element.type.names.length; i++) {
319+
if (element.type.names[i].toLowerCase() === 'undefined') {
320+
// This element is actually optional. Set optional to true and
321+
// remove the 'undefined' type
322+
element.optional = true;
323+
element.type.names.splice(i, 1);
324+
i--;
325+
}
326+
}
327+
}
328+
317329
if (seen[element.longname])
318330
return true;
319331
if (isClassLike(element)) {
@@ -460,7 +472,10 @@ function handleMember(element, parent) {
460472
} else
461473
write(element.kind === "constant" ? "const " : "var ");
462474

463-
write(element.name, ": ");
475+
write(element.name);
476+
if (element.optional)
477+
write("?");
478+
write(": ");
464479

465480
if (element.type && element.type.names && /^Object\b/i.test(element.type.names[0]) && element.properties) {
466481
writeln("{");

0 commit comments

Comments
 (0)