Skip to content

Commit

Permalink
fix: Correct handling of arrays in generic constraints
Browse files Browse the repository at this point in the history
Closes #1408
  • Loading branch information
Gerrit0 committed Dec 5, 2020
1 parent 76817dd commit d575dc0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
43 changes: 20 additions & 23 deletions scripts/rebuild_specs.js
Expand Up @@ -58,29 +58,26 @@ function rebuildConverterTests(dirs) {
return;
}

return Promise.all(
dirs.map((fullPath) => {
console.log(fullPath);
const src = app.expandInputFiles([fullPath]);
return Promise.all(
conversions.map(([file, before, after]) => {
const out = path.join(fullPath, `${file}.json`);
if (fs.existsSync(out)) {
TypeDoc.resetReflectionID();
before();
const result = app.converter.convert(src, program);
const serialized = app.serializer.toObject(result);

const data = JSON.stringify(serialized, null, " ")
.split(TypeDoc.normalizePath(base))
.join("%BASE%");
after();
return fs.writeFile(out.replace("dist", "src"), data);
}
})
);
})
);
for (const fullPath of dirs) {
console.log(fullPath);
const src = app.expandInputFiles([fullPath]);

for (const [file, before, after] of conversions) {
const out = path.join(fullPath, `${file}.json`);
if (fs.existsSync(out)) {
TypeDoc.resetReflectionID();
before();
const result = app.converter.convert(src, program);
const serialized = app.serializer.toObject(result);

const data = JSON.stringify(serialized, null, " ")
.split(TypeDoc.normalizePath(base))
.join("%BASE%");
after();
fs.writeFileSync(out.replace("dist", "src"), data);
}
}
}
}

async function rebuildRendererTest() {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/converter/types.ts
Expand Up @@ -149,7 +149,9 @@ const arrayConverter: TypeConverter<ts.ArrayTypeNode, ts.TypeReference> = {
},
convertType(context, type) {
const params = context.checker.getTypeArguments(type);
assert(params.length === 1);
// This is *almost* always true... except for when this type is in the constraint of a type parameter see GH#1408
// assert(params.length === 1);
assert(params.length > 0);
return new ArrayType(convertType(context, params[0]));
},
};
Expand Down
4 changes: 4 additions & 0 deletions src/test/converter/alias/alias.ts
Expand Up @@ -61,3 +61,7 @@ export namespace GH1330 {
declare const makeProp: <T>(x: T) => HasProp<T>;
export const testValue3 = makeProp(1);
}

export namespace GH1408 {
export declare function foo<T extends unknown[]>(): T;
}
57 changes: 56 additions & 1 deletion src/test/converter/alias/specs.json
Expand Up @@ -177,6 +177,60 @@
}
]
},
{
"id": 39,
"name": "GH1408",
"kind": 2,
"kindString": "Namespace",
"flags": {},
"children": [
{
"id": 40,
"name": "foo",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 41,
"name": "foo",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"typeParameter": [
{
"id": 42,
"name": "T",
"kind": 131072,
"kindString": "Type parameter",
"flags": {},
"type": {
"type": "array",
"elementType": {
"type": "intrinsic",
"name": "unknown"
}
}
}
],
"type": {
"type": "reference",
"name": "T"
}
}
]
}
],
"groups": [
{
"title": "Functions",
"kind": 64,
"children": [
40
]
}
]
},
{
"id": 21,
"name": "HorribleRecursiveTypeThatShouldNotBeUsedByAnyone",
Expand Down Expand Up @@ -684,7 +738,8 @@
"title": "Namespaces",
"kind": 2,
"children": [
28
28,
39
]
},
{
Expand Down

0 comments on commit d575dc0

Please sign in to comment.