Typedoc: Inaccurate JSON output types

Created on 1 Nov 2020  路  7Comments  路  Source: TypeStrong/typedoc

Hi, I'd like to get the data that TypeDoc generates when it parses a project and use it to convert it to a custom documentation format. The only way I could find is to generate a JSON file in a temp directory so that it can be parsed: the problem with that is that there are no typings for it, and so I can't be sure of all the properties it could have. I've tried manually assigning types from the package but they don't always match correctly, and I was ending up putting // @ts-expect-error everywhere...

Does anybody know how can I get this data directly from the package, before it gets converted to JSON?

bug

All 7 comments

TypeDoc exports a JSONOutput namespace with types for the JSON. The root object is a JSONOutput.ProjectReflection

Alternatively, to avoid writing out to disk in the first place, you could use TypeDoc's API, though be aware that this will be changing somewhat significantly in 0.20.

const TypeDoc = require('typedoc');

const app = new TypeDoc.Application();
app.options.addReader(new TypeDoc.TSConfigReader());
app.options.addReader(new TypeDoc.TypeDocReader());

app.bootstrap({
    tsconfig: 'tsconfig.json',
});

const project = app.convert(app.expandInputFiles(['src']));

if (project) { // Project may not have converted correctly
    const outputDir = 'docs';

    // Rendered docs
    app.generateDocs(project, outputDir);
    // Alternatively generate JSON output
    app.generateJson(project, outputDir + '/documentation.json');
}

Hi, thanks for the reply. I've tried using the JSONOutput.ProjectReflection typings, but they don't seem to correctly match the actual output. Here's an example: a class reflection in the generated JSON file has a property named extendedTypes, but the typings don't show it. There are multiple other properties with this problem.
I'm using the Reflection type for those classes, since it's the type you can extract from Exclude<JSONOutput.ProjectReflection['children'], undefined>[number]

That's a bug with those typings then, feel free to submit a PR!

Oh, ok, thanks. I'll close the issue for now.

@Gerrit0 I've found something interesting: JSONOutput.DeclarationReflection contains all the missing properties that I was mentioning. My question now is: shouldn't ProjectReflection automatically use DeclarationReflection for those typings? Do you already know if there's an issue with that, or is it only a bug in the typings?

I looked at the schema code again, and there are some very questionable choices for the types there... why is children on Reflection? That ought to live on ContainerReflection... I'll try and take a look at this next weekend.

Well... much later than I wanted to get to it, but I did finally get to it. v0.20.0-beta.13 has the corrected types. There was a cast to any in the generic serializer, which allowed making much of the serialization dynamic.... but essentially impossible to verify with types. This has now been removed.

Was this page helpful?
0 / 5 - 0 ratings