Skip to content

Commit 095bae6

Browse files
authored
fix(dts-generator): support rest parameters in functions in nested types (#507)
1.) When no param and no return type is given, generate "Function" instead of "()=>void" to allow passing parameters, just like it happens in simple function types. 2.) When api.json contains: Object<string,function(...any)>|function Then the d.ts file will contain: Record<string, (...p1: any) => void> | Function; Before this fix, the function had no parameter.
1 parent b6c0fe5 commit 095bae6

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,25 @@ function genType(ast: Type, usage: string = "unknown"): string {
867867
return intersectionTypes.join(" & ");
868868
case "FunctionType":
869869
text = "";
870-
if (ast.isConstructor) {
871-
text += "new ";
872-
}
873-
if (!_.isEmpty(ast.typeParameters)) {
874-
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
870+
if (
871+
!ast.isConstructor &&
872+
_.isEmpty(ast.typeParameters) &&
873+
_.isEmpty(ast.parameters) &&
874+
!ast.type
875+
) {
876+
// for simple functions, generate "Function" type
877+
text += "Function";
878+
} else {
879+
// generate arrow function type
880+
if (ast.isConstructor) {
881+
text += "new ";
882+
}
883+
if (!_.isEmpty(ast.typeParameters)) {
884+
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
885+
}
886+
text += `(${_.map(ast.parameters, (param) => `${param.repeatable ? "..." : ""}${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
887+
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
875888
}
876-
text += `(${_.map(ast.parameters, (param) => `${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
877-
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
878889
return text;
879890
case "NativeTSTypeExpression":
880891
// native TS type expression, emit the 'type' string "as is"

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

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

test-packages/openui5-snapshot-test/output-dts/sap.ui.core.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83938,11 +83938,11 @@ declare module "sap/ui/test/Opa5" {
8393883938
* whether a function is used as arrangement or action. Each function typically contains one or multiple
8393983939
* `waitFor` statements.
8394083940
*/
83941-
actions?: Record<string, () => void> | Function;
83941+
actions?: Record<string, Function> | Function;
8394283942
/**
8394383943
* A map or a class of functions that can be used as assertions in Opa tests.
8394483944
*/
83945-
assertions?: Record<string, () => void> | Function;
83945+
assertions?: Record<string, Function> | Function;
8394683946
};
8394783947
}
8394883948

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy