Skip to content

Commit 43cbf32

Browse files
committed
TypeScript: Fix @remarks used for full instead of simplified type form
1 parent 65deef6 commit 43cbf32

File tree

9 files changed

+70
-30
lines changed

9 files changed

+70
-30
lines changed

.changeset/large-ducks-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'fumadocs-typescript': patch
3+
---
4+
5+
Fix `@remarks` used for full instead of simplified type form

apps/docs/content/docs/ui/(ui)/components/files.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ project
5959

6060
### File
6161

62-
<AutoTypeTable path="./content/docs/ui/props.ts" name="FileProps" />
62+
<auto-type-table path="../../props.ts" name="FileProps" />
6363

6464
### Folder
6565

66-
<AutoTypeTable path="./content/docs/ui/props.ts" name="FolderProps" />
66+
<auto-type-table path="../../props.ts" name="FolderProps" />

packages/typescript/src/lib/base.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export function createGenerator(config?: GeneratorOptions | Project) {
8686
project: config,
8787
}
8888
: config;
89-
const cacheType = options?.cache ?? 'fs';
89+
const cacheType =
90+
options?.cache ?? (process.env.NODE_ENV !== 'development' ? 'fs' : false);
9091
const cache = cacheType === 'fs' ? createCache() : null;
9192
let instance: Project | undefined;
9293

@@ -214,13 +215,18 @@ function getDocEntry(
214215
}) satisfies RawTag,
215216
);
216217

217-
let type = getFullType(subType);
218+
let simplifiedType = getSimpleForm(
219+
subType,
220+
program.getTypeChecker(),
221+
isOptional,
222+
);
218223

219-
for (const tag of tags) {
220-
if (tag.name !== 'remarks') continue;
224+
const remarksTag = tags.find((tag) => tag.name === 'remarks');
225+
if (remarksTag) {
226+
const match = /^`(?<name>.+)`/.exec(remarksTag.text)?.[1];
221227

222228
// replace type with @remarks
223-
type = /^`(?<name>.+)`/.exec(tag.text)?.[1] ?? type;
229+
if (match) simplifiedType = match;
224230
}
225231

226232
const entry: DocEntry = {
@@ -231,12 +237,8 @@ function getDocEntry(
231237
),
232238
),
233239
tags,
234-
type,
235-
simplifiedType: getSimpleForm(
236-
subType,
237-
program.getTypeChecker(),
238-
isOptional,
239-
),
240+
type: getFullType(subType),
241+
simplifiedType,
240242
required: !isOptional,
241243
deprecated: tags.some((tag) => tag.name === 'deprecated'),
242244
};
@@ -247,16 +249,18 @@ function getDocEntry(
247249
}
248250

249251
function getFullType(type: Type): string {
250-
let typeName = type.getText(
252+
const alias = type.getAliasSymbol();
253+
if (alias) {
254+
return alias
255+
.getDeclaredType()
256+
.getText(undefined, ts.TypeFormatFlags.NoTruncation);
257+
}
258+
259+
return type.getText(
251260
undefined,
252261
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |
253262
ts.TypeFormatFlags.NoTruncation |
263+
// we use `InTypeAlias` to force TypeScript to extend generic types like `ExtractParams<T>` into more detailed forms like `T extends string? ExtractParamsFromString<T> : never`.
254264
ts.TypeFormatFlags.InTypeAlias,
255265
);
256-
257-
if (type.getAliasSymbol() && type.getAliasTypeArguments().length === 0) {
258-
typeName = type.getAliasSymbol()?.getEscapedName() ?? typeName;
259-
}
260-
261-
return typeName;
262266
}

packages/typescript/src/lib/get-simple-form.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ export function getSimpleForm(
88
if (type.isUndefined() && noUndefined) return '';
99

1010
const alias = type.getAliasSymbol();
11-
if (alias && type.getAliasTypeArguments().length === 0) {
12-
return alias.getEscapedName();
11+
if (alias) {
12+
return alias
13+
.getDeclaredType()
14+
.getText(
15+
undefined,
16+
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope,
17+
);
1318
}
1419

1520
if (type.isUnion()) {
@@ -58,7 +63,6 @@ export function getSimpleForm(
5863

5964
return type.getText(
6065
undefined,
61-
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |
62-
ts.TypeFormatFlags.InTypeAlias,
66+
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope,
6367
);
6468
}

packages/typescript/src/lib/mdx.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ ${doc.description}
3434
3535
<div className="flex flex-row items-center gap-4">
3636
<code className="text-sm">${c.name}</code>
37-
<code className="text-fd-muted-foreground">{${JSON.stringify(c.type)}}</code>
37+
<code className="text-fd-muted-foreground">{${JSON.stringify(c.simplifiedType)}}</code>
3838
</div>
3939
40+
Full Type: <code className="text-fd-muted-foreground">{${JSON.stringify(c.type)}}</code>
41+
4042
${c.description || 'No Description'}
4143
4244
${c.tags

packages/typescript/test/fixtures/test.output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function _createMdxContent(props) {
6464
"--shiki-light": "#24292E",
6565
"--shiki-dark": "#E1E4E8"
6666
},
67-
children: "number"
67+
children: "timestamp"
6868
})
6969
})
7070
}),
@@ -76,7 +76,7 @@ function _createMdxContent(props) {
7676
"--shiki-light": "#24292E",
7777
"--shiki-dark": "#E1E4E8"
7878
},
79-
children: "timestamp"
79+
children: "number"
8080
})
8181
})
8282
}),

packages/typescript/test/fixtures/test.output.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101
"text": "`timestamp`\nReturned by API"
102102
}
103103
],
104-
"type": "timestamp",
105-
"simplifiedType": "number",
104+
"type": "number",
105+
"simplifiedType": "timestamp",
106106
"required": true,
107107
"deprecated": false
108108
},
@@ -121,5 +121,20 @@
121121
"deprecated": false
122122
}
123123
]
124+
},
125+
{
126+
"name": "Test4",
127+
"description": "",
128+
"entries": [
129+
{
130+
"name": "prop",
131+
"description": "",
132+
"tags": [],
133+
"type": "Complicated<T>",
134+
"simplifiedType": "Complicated<T>",
135+
"required": true,
136+
"deprecated": false
137+
}
138+
]
124139
}
125140
]

packages/typescript/test/fixtures/test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ interface GenericType<A, B, C> {
1717
B: B;
1818
C: C;
1919
}
20+
21+
export type Test4<T> = {
22+
get prop(): Complicated<T>;
23+
};
24+
25+
type Complicated<T> = T extends { [K in keyof T]: T[K] }
26+
? {
27+
[K in keyof T as Lowercase<string & K>]: T[K];
28+
}
29+
: never;

packages/typescript/test/type-gen.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const generator = createGenerator(tsconfig);
2424
test('Run', async () => {
2525
const file = relative('./fixtures/test.ts');
2626

27-
const result = ['Test1', 'Test2', 'Test3'].flatMap((name) =>
27+
const result = ['Test1', 'Test2', 'Test3', 'Test4'].flatMap((name) =>
2828
generator.generateDocumentation({ path: file }, name),
2929
);
3030

0 commit comments

Comments
 (0)