/
index-signature.ts
55 lines (52 loc) · 1.83 KB
/
index-signature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as assert from "assert";
import * as ts from "typescript";
import {
DeclarationReflection,
ParameterReflection,
ReflectionKind,
SignatureReflection,
} from "../../models";
import { Context } from "../context";
import { ConverterEvents } from "../converter-events";
export function convertIndexSignature(context: Context, symbol: ts.Symbol) {
assert(context.scope instanceof DeclarationReflection);
const indexSymbol = symbol.members?.get("__index" as ts.__String);
if (indexSymbol) {
// Right now TypeDoc models don't have a way to distinguish between string
// and number index signatures... { [x: string]: 1 | 2; [x: number]: 2 }
// will be misrepresented.
const indexDeclaration = indexSymbol.getDeclarations()?.[0];
assert(
indexDeclaration && ts.isIndexSignatureDeclaration(indexDeclaration)
);
const param = indexDeclaration.parameters[0];
assert(param && ts.isParameter(param));
const index = new SignatureReflection(
"__index",
ReflectionKind.IndexSignature,
context.scope
);
index.parameters = [
new ParameterReflection(
param.name.getText(),
ReflectionKind.Parameter,
index
),
];
index.parameters[0].type = context.converter.convertType(
context.withScope(index.parameters[0]),
param.type
);
index.type = context.converter.convertType(
context.withScope(index),
indexDeclaration.type
);
context.registerReflection(index, indexSymbol);
context.scope.indexSignature = index;
context.trigger(
ConverterEvents.CREATE_SIGNATURE,
index,
indexDeclaration
);
}
}