Skip to content

Commit f50408d

Browse files
authored
improve ts-info type text (#126)
1 parent 80b6125 commit f50408d

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

packages/playground/use-theme-doc/pages/components/button/index$.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ SomeObjectLiteralType (Object Literal):
1717
SomeComplexType (Complex Type):
1818
<TsInfo src="./types.ts" name="SomeComplexType" />
1919

20+
Re-exported Type:
21+
<TsInfo src="./types.ts" name="ReExportedInterface" />
22+
2023
FileText:
2124
<FileText src="./types.ts" syntax="ts" />

packages/playground/use-theme-doc/pages/components/button/types.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
import type { MyImportedTypeAlias } from './typesUtils'
2+
export type { ReExportedInterface } from './typesUtils'
3+
export type MyExportedTypeAlias = { a: number }
4+
type MyTypeAlias = { a: number }
5+
export interface MyExportedInterface {
6+
a: number
7+
}
8+
interface MyInterface {
9+
a: number
10+
}
11+
112
/**
213
* This is the description of the Button component's props
314
*/
@@ -22,9 +33,14 @@ export interface ButtonProps<TestGenerics extends string> extends Base {
2233
*/
2334
onClick?: (event: React.MouseEvent) => void
2435
/** test method declaration */
25-
testMethod(param: string): void
36+
testMethod(param: MyExportedTypeAlias): MyTypeAlias
2637
/** test required property */
2738
testRequired: boolean
39+
myExportedTypeAlias: MyExportedTypeAlias
40+
myTypeAlias: MyTypeAlias
41+
myExportedInterface: MyExportedInterface
42+
myInterface: MyInterface
43+
myImportedTypeAlias: MyImportedTypeAlias
2844
}
2945

3046
interface Base {
@@ -55,9 +71,14 @@ export type SomeObjectLiteralType<TestGenerics> = {
5571
*/
5672
onClick?: (event: React.MouseEvent) => void
5773
/** test method declaration */
58-
testMethod(param: string): void
74+
testMethod(param: MyInterface): MyExportedInterface
5975
/** test required property */
6076
testRequired: boolean
77+
myExportedTypeAlias: MyExportedTypeAlias
78+
myTypeAlias: MyTypeAlias
79+
myExportedInterface: MyExportedInterface
80+
myInterface: MyInterface
81+
myImportedTypeAlias: MyImportedTypeAlias
6182
}
6283

6384
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type MyImportedTypeAlias = { b: string }
2+
/**
3+
* Comment for MyImportedInterface...
4+
*/
5+
export type ReExportedInterface = {
6+
/** Comment for MyImportedInterface.prop1 */
7+
prop1: MyImportedTypeAlias
8+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// import the src directly instead of build output
22
// useful for local development in this repo
33
// actual users don't need to do this, they should import from 'vite-pages-theme-doc'
4-
// export * from 'vite-pages-theme-doc/src/index'
5-
export * from 'vite-pages-theme-doc'
4+
export * from 'vite-pages-theme-doc/src/index'
5+
// export * from 'vite-pages-theme-doc'

packages/react-pages/src/node/virtual-module-plugins/ts-info-module/extract.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ function handleTypeElementMembered(
138138
typeChecker: TypeChecker
139139
): TsPropertyOrMethodInfo[] {
140140
const result: TsPropertyOrMethodInfo[] = []
141+
// or use node.getSymbol()?.getMembers() ?
141142
const nodeType = node.getType()
142143
const properties = nodeType.getProperties()
143144
for (const prop of properties) {
144145
const name = prop.getName()
145146
const description = ts.displayPartsToString(
146147
prop.compilerSymbol.getDocumentationComment(typeChecker.compilerObject)
147148
)
148-
const type = prop.getTypeAtLocation(node).getText()
149+
const type = prop
150+
.getTypeAtLocation(node)
151+
// drop the `import('/path/to/file').` before the type text
152+
// https://github.com/dsherret/ts-morph/issues/453#issuecomment-667578386
153+
.getText(node, ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope)
149154
const defaultValue = (() => {
150155
let res = ''
151156
prop.getJsDocTags().find((tag) => {

0 commit comments

Comments
 (0)