Skip to content

Commit d575dc0

Browse files
committed
fix: Correct handling of arrays in generic constraints
Closes #1408
1 parent 76817dd commit d575dc0

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

scripts/rebuild_specs.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,26 @@ function rebuildConverterTests(dirs) {
5858
return;
5959
}
6060

61-
return Promise.all(
62-
dirs.map((fullPath) => {
63-
console.log(fullPath);
64-
const src = app.expandInputFiles([fullPath]);
65-
return Promise.all(
66-
conversions.map(([file, before, after]) => {
67-
const out = path.join(fullPath, `${file}.json`);
68-
if (fs.existsSync(out)) {
69-
TypeDoc.resetReflectionID();
70-
before();
71-
const result = app.converter.convert(src, program);
72-
const serialized = app.serializer.toObject(result);
73-
74-
const data = JSON.stringify(serialized, null, " ")
75-
.split(TypeDoc.normalizePath(base))
76-
.join("%BASE%");
77-
after();
78-
return fs.writeFile(out.replace("dist", "src"), data);
79-
}
80-
})
81-
);
82-
})
83-
);
61+
for (const fullPath of dirs) {
62+
console.log(fullPath);
63+
const src = app.expandInputFiles([fullPath]);
64+
65+
for (const [file, before, after] of conversions) {
66+
const out = path.join(fullPath, `${file}.json`);
67+
if (fs.existsSync(out)) {
68+
TypeDoc.resetReflectionID();
69+
before();
70+
const result = app.converter.convert(src, program);
71+
const serialized = app.serializer.toObject(result);
72+
73+
const data = JSON.stringify(serialized, null, " ")
74+
.split(TypeDoc.normalizePath(base))
75+
.join("%BASE%");
76+
after();
77+
fs.writeFileSync(out.replace("dist", "src"), data);
78+
}
79+
}
80+
}
8481
}
8582

8683
async function rebuildRendererTest() {

src/lib/converter/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ const arrayConverter: TypeConverter<ts.ArrayTypeNode, ts.TypeReference> = {
149149
},
150150
convertType(context, type) {
151151
const params = context.checker.getTypeArguments(type);
152-
assert(params.length === 1);
152+
// This is *almost* always true... except for when this type is in the constraint of a type parameter see GH#1408
153+
// assert(params.length === 1);
154+
assert(params.length > 0);
153155
return new ArrayType(convertType(context, params[0]));
154156
},
155157
};

src/test/converter/alias/alias.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ export namespace GH1330 {
6161
declare const makeProp: <T>(x: T) => HasProp<T>;
6262
export const testValue3 = makeProp(1);
6363
}
64+
65+
export namespace GH1408 {
66+
export declare function foo<T extends unknown[]>(): T;
67+
}

src/test/converter/alias/specs.json

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,60 @@
177177
}
178178
]
179179
},
180+
{
181+
"id": 39,
182+
"name": "GH1408",
183+
"kind": 2,
184+
"kindString": "Namespace",
185+
"flags": {},
186+
"children": [
187+
{
188+
"id": 40,
189+
"name": "foo",
190+
"kind": 64,
191+
"kindString": "Function",
192+
"flags": {},
193+
"signatures": [
194+
{
195+
"id": 41,
196+
"name": "foo",
197+
"kind": 4096,
198+
"kindString": "Call signature",
199+
"flags": {},
200+
"typeParameter": [
201+
{
202+
"id": 42,
203+
"name": "T",
204+
"kind": 131072,
205+
"kindString": "Type parameter",
206+
"flags": {},
207+
"type": {
208+
"type": "array",
209+
"elementType": {
210+
"type": "intrinsic",
211+
"name": "unknown"
212+
}
213+
}
214+
}
215+
],
216+
"type": {
217+
"type": "reference",
218+
"name": "T"
219+
}
220+
}
221+
]
222+
}
223+
],
224+
"groups": [
225+
{
226+
"title": "Functions",
227+
"kind": 64,
228+
"children": [
229+
40
230+
]
231+
}
232+
]
233+
},
180234
{
181235
"id": 21,
182236
"name": "HorribleRecursiveTypeThatShouldNotBeUsedByAnyone",
@@ -684,7 +738,8 @@
684738
"title": "Namespaces",
685739
"kind": 2,
686740
"children": [
687-
28
741+
28,
742+
39
688743
]
689744
},
690745
{

0 commit comments

Comments
 (0)