@@ -6,37 +6,11 @@ import {
66 ts ,
77} from 'ts-morph'
88
9- export type TsInfo =
10- | {
11- // example: type A = { k: v }
12- type : 'object-literal'
13- name : string
14- description : string
15- properties : TsPropertyOrMethodInfo [ ]
16- }
17- | {
18- // example: interface MyInterface { k: v }
19- type : 'interface'
20- name : string
21- description : string
22- properties : TsPropertyOrMethodInfo [ ]
23- }
24- | {
25- // complex type literal
26- // example: type A = 'asd' | 123
27- type : 'other'
28- name : string
29- description : string
30- text : string
31- }
32-
33- export interface TsPropertyOrMethodInfo {
34- name : string
35- type : string
36- description : string
37- defaultValue : string | undefined
38- optional : boolean
39- }
9+ import type {
10+ TsInfo ,
11+ TsPropertyOrMethodInfo ,
12+ CallSignatureInfo ,
13+ } from '../../../../clientTypes'
4014
4115const defaultTsConfig : ts . CompilerOptions = {
4216 target : ts . ScriptTarget . ESNext ,
@@ -92,12 +66,15 @@ export function collectInterfaceInfo(
9266
9367 if ( Node . isTypeLiteral ( typeNode ) ) {
9468 // example: type A = { k: v }
95- const members = handleTypeElementMembered ( typeNode , typeChecker )
69+ const { members, callSignatures, constructSignatures } =
70+ handleTypeElementMembered ( typeNode , typeChecker )
9671 return {
9772 type : 'object-literal' ,
9873 name,
9974 description,
10075 properties : members ,
76+ callSignatures,
77+ constructSignatures,
10178 }
10279 } else {
10380 // example: type A = 'asd' | 123
@@ -122,8 +99,16 @@ export function collectInterfaceInfo(
12299 return jsDoc . getDescription ( ) . trim ( )
123100 } )
124101 . join ( '\n\n' )
125- const members = handleTypeElementMembered ( node , typeChecker )
126- return { type : 'interface' , name, description, properties : members }
102+ const { members, callSignatures, constructSignatures } =
103+ handleTypeElementMembered ( node , typeChecker )
104+ return {
105+ type : 'interface' ,
106+ name,
107+ description,
108+ properties : members ,
109+ callSignatures,
110+ constructSignatures,
111+ }
127112 }
128113
129114 throw new Error ( 'unexpected node type: ' + node . getKindName ( ) )
@@ -136,12 +121,16 @@ export function collectInterfaceInfo(
136121function handleTypeElementMembered (
137122 node : TypeElementMemberedNode & Node ,
138123 typeChecker : TypeChecker
139- ) : TsPropertyOrMethodInfo [ ] {
140- const result : TsPropertyOrMethodInfo [ ] = [ ]
124+ ) : {
125+ members : TsPropertyOrMethodInfo [ ]
126+ callSignatures : CallSignatureInfo [ ]
127+ constructSignatures : CallSignatureInfo [ ]
128+ } {
129+ const members : TsPropertyOrMethodInfo [ ] = [ ]
141130 // or use node.getSymbol()?.getMembers() ?
142131 const nodeType = node . getType ( )
143- const properties = nodeType . getProperties ( )
144- for ( const prop of properties ) {
132+ // https://stackoverflow.com/a/68623960
133+ for ( const prop of nodeType . getProperties ( ) ) {
145134 const name = prop . getName ( )
146135 const description = ts . displayPartsToString (
147136 prop . compilerSymbol . getDocumentationComment ( typeChecker . compilerObject )
@@ -165,15 +154,40 @@ function handleTypeElementMembered(
165154 return res
166155 } ) ( )
167156 const optional = prop . isOptional ( )
168- result . push ( {
157+ members . push ( {
169158 name,
170159 description,
171160 type,
172161 defaultValue,
173162 optional,
174163 } )
175164 }
176- return result
165+
166+ const callSignatures : CallSignatureInfo [ ] = [ ]
167+ for ( const sig of nodeType . getCallSignatures ( ) ) {
168+ const description = ts . displayPartsToString (
169+ sig . compilerSignature . getDocumentationComment ( typeChecker . compilerObject )
170+ )
171+ const type = sig . getDeclaration ( ) . getText ( )
172+ callSignatures . push ( {
173+ description,
174+ type,
175+ } )
176+ }
177+
178+ const constructSignatures : CallSignatureInfo [ ] = [ ]
179+ for ( const sig of nodeType . getConstructSignatures ( ) ) {
180+ const description = ts . displayPartsToString (
181+ sig . compilerSignature . getDocumentationComment ( typeChecker . compilerObject )
182+ )
183+ const type = sig . getDeclaration ( ) . getText ( )
184+ constructSignatures . push ( {
185+ description,
186+ type,
187+ } )
188+ }
189+
190+ return { members, callSignatures, constructSignatures }
177191}
178192
179193// an alternative way to implement handleTypeElementMembered
0 commit comments