-
Notifications
You must be signed in to change notification settings - Fork 699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to expand type aliases #1258
Comments
See also #1021 |
Have a similar requirement. Will be great to have a flag or a plugin (as suggested in #1021).
export const RESTFontWeight = {
Bold: "bold",
Bolder: "bolder",
Lighter: "lighter",
Normal: "normal"
} as const;
export type RESTFontWeight = typeof RESTFontWeight[keyof typeof RESTFontWeight];
|
Hi @Gerrit0, in a similar example also noticed a possible bug (Using : Source Code: export const MyObject =
{
myValue: "ddddd"
} as const;
export type MyType = typeof MyObject.myValue; TS Intelisense: TypeDoc behavior: Issue: |
Sounds like contradictory requests here ;) "resolve the type" vs "preserve the source representation" Since we do generate a link to |
Yeah they are contradictory :) pointed out because I thought "preserve the source representation" should be consistent throughout.
Yeah, that would be quite acceptable too but it's not happening. It took me to the URL: |
Good to know about the broken links, I guess I should have expected that... Its kind of amazing how many links do work given the piecemeal way we build them up today. |
I'm very much looking to do the same thing as OP. Any guidance on how to approach this, including guidance on how to write a plugin for this is welcome. |
Doing this is rather tricky. I think the right solution would be to create a plugin which listens to I'm afraid I can't be more helpful here, its been a while since I looked at how this might be doable. |
@Gerrit0 Thanks for the suggestion. I tried going down a similar road, but it indeed does get amazingly complex very quickly. The fact that I haven't worked with TS's compiler, or any compiler of similar magnitude not helping. Ultimately, I went down another road, and found a much simpler way to get what I needed: I wrote a small utility to extract static types from cc @moltar |
Far from perfect, but this gets the job done if you don't care about extra information that TypeDoc keeps about type info when rendering docs (enabling links, syntax highlighting) //@ts-check
// Supports: typedoc@0.20
const { Converter, ReflectionKind, TypeScript: ts } = require("typedoc");
const { UnknownType } = require("typedoc/dist/lib/models");
/** @param {import("typedoc").Application} param0 */
exports.load = function ({ application }) {
const printer = ts.createPrinter();
/** @type {Map<import("typedoc").DeclarationReflection, UnknownType>} */
const typeOverrides = new Map();
application.converter.on(
Converter.EVENT_CREATE_DECLARATION,
/**
*
* @param {import("typedoc/dist/lib/converter/context").Context} context
* @param {import("typedoc").DeclarationReflection} reflection
* @param {import("typescript").Node | undefined} node
*/
(context, reflection, node) => {
if (reflection.kind === ReflectionKind.TypeAlias && node) {
if (reflection.comment && reflection.comment.hasTag("quickinfo")) {
reflection.comment.removeTags("quickinfo");
const type = context.checker.getTypeAtLocation(node);
const typeNode = context.checker.typeToTypeNode(
type,
node.getSourceFile(),
ts.NodeBuilderFlags.InTypeAlias
);
typeOverrides.set(
reflection,
new UnknownType(
printer.printNode(
ts.EmitHint.Unspecified,
typeNode,
node.getSourceFile()
)
)
);
}
}
}
);
application.converter.on(Converter.EVENT_RESOLVE_BEGIN, () => {
for (const [refl, type] of typeOverrides) {
refl.type = type;
}
typeOverrides.clear();
});
}; Then, with this source: export interface A {
x: string
}
/** @quickInfo */
export type Mapped = {
[K in keyof A & string as `${K}.${K}`]: A[K]
} TypeDoc will produce: Replacing the call to create a new |
@Gerrit0 Please could you supply a license for the above plugin? Thanks 🙏 |
Public domain, do what you will :) |
@Gerrit0 the above plugin no longer works with TypeDoc 0.23.25. I looked into the source and |
For node, you'll need to get that from the symbol yourself now - |
Thanks @Gerrit0 for the pointer! Below is the updated code that works with //@ts-check
// Supports: typedoc@0.23
const {
Converter,
ReflectionKind,
TypeScript: ts,
UnknownType,
} = require('typedoc');
/** @param {import("typedoc").Application} param0 */
exports.load = function ({ application }) {
const printer = ts.createPrinter();
/** @type {Map<import("typedoc").DeclarationReflection, UnknownType>} */
const typeOverrides = new Map();
application.converter.on(
Converter.EVENT_CREATE_DECLARATION,
/**
*
* @param {import("typedoc/dist/lib/converter/context").Context} context
* @param {import("typedoc").DeclarationReflection} reflection
*/
(context, reflection) => {
const node =
context.project.getSymbolFromReflection(reflection)?.declarations?.[0];
if (reflection.kind === ReflectionKind.TypeAlias && node) {
if (reflection.comment?.getTag('@quickinfo')) {
reflection.comment.removeTags('@quickinfo');
const type = context.checker.getTypeAtLocation(node);
const typeNode = context.checker.typeToTypeNode(
type,
node.getSourceFile(),
ts.NodeBuilderFlags.InTypeAlias,
);
typeOverrides.set(
reflection,
new UnknownType(
printer.printNode(
ts.EmitHint.Unspecified,
typeNode,
node.getSourceFile(),
),
),
);
}
}
},
);
application.converter.on(Converter.EVENT_RESOLVE_BEGIN, () => {
for (const [refl, type] of typeOverrides) {
refl.type = type;
}
typeOverrides.clear();
});
}; |
Problem
I'm using a library to create types that hsa runtime checking: io-ts
Types are declared like so:
Then in the docs, it shows the type as:
Which isn't helpful to the reader.
Suggested Solution
I propose to have a flag that expands all of the aliases into a full type / interface.
E.g.
It would expand the type into:
The text was updated successfully, but these errors were encountered: