Skip to content

Commit 6ca76e2

Browse files
committed
feat(dts-generator): support optional parameters in function types
1 parent adb6a33 commit 6ca76e2

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

packages/dts-generator/src/phases/dts-code-gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ function genType(ast: Type, usage: string = "unknown"): string {
867867
if (!_.isEmpty(ast.typeParameters)) {
868868
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
869869
}
870-
text += `(${_.map(ast.parameters, (param) => `${param.name}: ${genType(param.type, "parameter")}`).join(", ")})`;
870+
text += `(${_.map(ast.parameters, (param) => `${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
871871
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
872872
return text;
873873
case "NativeTSTypeExpression":

packages/dts-generator/src/utils/ts-ast-type-builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class TSASTTypeBuilder {
114114
kind: "Parameter",
115115
name: "p" + (idx + 1), // JSDoc function types don't allow parameter names -> generate names
116116
type: param,
117+
optional: (param as any).optional,
117118
}));
118119
if (thisType != null) {
119120
// for TS, a 'this' type is specified as the first parameter type of a function

0 commit comments

Comments
 (0)