Skip to content

Commit 27ce957

Browse files
authored
revert: support constructor types and option parameters in function types (#485) (#486)
This reverts commit 77f8226.
1 parent 77f8226 commit 27ce957

File tree

4 files changed

+11
-29
lines changed

4 files changed

+11
-29
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,13 +861,10 @@ function genType(ast: Type, usage: string = "unknown"): string {
861861
return intersectionTypes.join(" & ");
862862
case "FunctionType":
863863
text = "";
864-
if (ast.isConstructor) {
865-
text += "new ";
866-
}
867864
if (!_.isEmpty(ast.typeParameters)) {
868865
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
869866
}
870-
text += `(${_.map(ast.parameters, (param) => `${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
867+
text += `(${_.map(ast.parameters, (param) => `${param.name}: ${genType(param.type, "parameter")}`).join(", ")})`;
871868
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
872869
return text;
873870
case "NativeTSTypeExpression":

packages/dts-generator/src/types/ast.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ export interface FunctionType {
238238
parameters: Parameter[];
239239
typeParameters?: TypeParameter[];
240240
type?: Type;
241-
isConstructor?: boolean;
242241
}
243242

244243
export interface LiteralType {

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
TypeReference,
77
UnionType,
88
TypeLiteral,
9-
Parameter,
109
} from "../types/ast.js";
1110

1211
/**
@@ -110,25 +109,18 @@ export class TSASTTypeBuilder {
110109
thisType: Type,
111110
constructorType: Type,
112111
): FunctionType {
113-
const parameters: Parameter[] = paramTypes.map((param, idx) => ({
114-
kind: "Parameter",
115-
name: "p" + (idx + 1), // JSDoc function types don't allow parameter names -> generate names
116-
type: param,
117-
optional: (param as any).optional,
118-
}));
119-
if (thisType != null) {
120-
// for TS, a 'this' type is specified as the first parameter type of a function
121-
parameters.unshift({
122-
kind: "Parameter",
123-
name: "this",
124-
type: thisType,
125-
});
126-
}
127112
return {
128113
kind: "FunctionType",
129-
parameters,
130-
type: constructorType ?? returnType,
131-
isConstructor: constructorType != null,
114+
parameters: paramTypes.map((param, idx) => ({
115+
kind: "Parameter",
116+
name: "p" + (idx + 1), // JSDoc function types don't allow parameter names -> generate names
117+
type: param,
118+
})),
119+
type: returnType,
120+
/* TODO not supported yet:
121+
"this": thisType,
122+
constructor: constructorType
123+
*/
132124
};
133125
}
134126
structure(structure: {

packages/dts-generator/src/utils/type-parser.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ export function TypeParser(
134134
next(":");
135135
returnType = parseType();
136136
}
137-
if (constructorType != null && returnType != null) {
138-
throw new SyntaxError(
139-
`A function signature must either use the 'new' keyword or have a return type, ` +
140-
`but not both (pos: ${rLexer.lastIndex}, input='${input}')`,
141-
);
142-
}
143137
type = builder.function(
144138
paramTypes,
145139
returnType,

0 commit comments

Comments
 (0)