Skip to content

fix(dts-generator): support rest parameters in functions in nested types #507

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions packages/dts-generator/src/phases/dts-code-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,14 +867,25 @@ function genType(ast: Type, usage: string = "unknown"): string {
return intersectionTypes.join(" & ");
case "FunctionType":
text = "";
if (ast.isConstructor) {
text += "new ";
}
if (!_.isEmpty(ast.typeParameters)) {
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
if (
!ast.isConstructor &&
_.isEmpty(ast.typeParameters) &&
_.isEmpty(ast.parameters) &&
!ast.type
) {
// for simple functions, generate "Function" type
text += "Function";
} else {
// generate arrow function type
if (ast.isConstructor) {
text += "new ";
}
if (!_.isEmpty(ast.typeParameters)) {
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
}
text += `(${_.map(ast.parameters, (param) => `${param.type?.repeatable ? "..." : ""}${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
}
text += `(${_.map(ast.parameters, (param) => `${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
return text;
case "NativeTSTypeExpression":
// native TS type expression, emit the 'type' string "as is"
Expand Down
5 changes: 3 additions & 2 deletions packages/dts-generator/src/types/ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ export interface ExperimentalDesc extends Description {

// Types

export type Type =
export type Type = (
| TypeReference
| TypeLiteral
| UnionType
| IntersectionType
| ArrayType
| FunctionType
| LiteralType
| NativeTSTypeExpression;
| NativeTSTypeExpression
) & { repeatable?: boolean };

export interface TypeReference {
kind: "TypeReference";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83938,11 +83938,11 @@ declare module "sap/ui/test/Opa5" {
* whether a function is used as arrangement or action. Each function typically contains one or multiple
* `waitFor` statements.
*/
actions?: Record<string, () => void> | Function;
actions?: Record<string, Function> | Function;
/**
* A map or a class of functions that can be used as assertions in Opa tests.
*/
assertions?: Record<string, () => void> | Function;
assertions?: Record<string, Function> | Function;
};
}

Expand Down