Skip to content

Commit d1b07c4

Browse files
devongovettdanez
authored andcommitted
Added support for TypeScript, Flow interfaces and Flow generic types (#348)
* Support parsing typescript with babel * Process TypeScript annotations * Support typescript and flow interfaces * Add composes for unresolved interface extensions * Move TypeScript output to tsType instead of flowType * Add support for flow and typescript generic type parameters * Support method parameters in TypeScript * Support rest parameters in function signatures * Bump recast * Separate TS `this` parameters from arguments * Code review comments * Fix tests for latests babel parser and yarn lock
1 parent c9a1556 commit d1b07c4

31 files changed

+2185
-124
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ we are getting this output:
239239
}
240240
```
241241

242-
## Flow Type support
242+
## Flow and TypeScript support
243243

244-
If you are using [flow][flow] then react-docgen can also extract the flow type annotations. As flow has a way more advanced and fine granular type system, the returned types from react-docgen are different in comparison when using `React.PropTypes`.
244+
If you are using [flow][flow] or [typescript][typescript] then react-docgen can also extract the type annotations. As flow and typescript have way more advanced and fine granular type systems, the returned types from react-docgen are different in comparison when using `React.PropTypes`.
245245

246246
> **Note**: react-docgen will not be able to grab the type definition if the type is imported or declared in a different file.
247247
248248
### Example
249249

250-
For the following component
250+
For the following component with Flow types
251251

252252
```js
253253
import React, { Component } from 'react';
@@ -387,6 +387,7 @@ The structure of the JSON blob / JavaScript object is as follows:
387387
["raw": string]
388388
},
389389
"flowType": <flowType>,
390+
"tsType": <tsType>,
390391
"required": boolean,
391392
"description": string,
392393
["defaultValue": {
@@ -406,9 +407,11 @@ The structure of the JSON blob / JavaScript object is as follows:
406407
- `<typeName>`: The name of the type, which is usually corresponds to the function name in `React.PropTypes`. However, for types define with `oneOf`, we use `"enum"` and for `oneOfType` we use `"union"`. If a custom function is provided or the type cannot be resolved to anything of `React.PropTypes`, we use `"custom"`.
407408
- `<typeValue>`: Some types accept parameters which define the type in more detail (such as `arrayOf`, `instanceOf`, `oneOf`, etc). Those are stored in `<typeValue>`. The data type of `<typeValue>` depends on the type definition.
408409
- `<flowType>`: If using flow type this property contains the parsed flow type as can be seen in the table above.
410+
- `<tsType>`: If using TypeScript type this property contains the parsed TypeScript type as can be seen in the table above.
409411

410412
[react]: http://facebook.github.io/react/
411413
[flow]: http://flowtype.org/
414+
[typescript]: http://typescriptlang.org/
412415
[recast]: https://github.com/benjamn/recast
413416
[@babel/parser]: https://github.com/babel/babel/tree/master/packages/babel-parser
414417
[classes]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,16 @@
3737
"author": "Felix Kling",
3838
"license": "MIT",
3939
"dependencies": {
40-
"@babel/core": "^7.0.0",
40+
"@babel/core": "^7.4.4",
4141
"@babel/runtime": "^7.0.0",
4242
"async": "^2.1.4",
4343
"commander": "^2.19.0",
4444
"doctrine": "^3.0.0",
4545
"node-dir": "^0.1.10",
46-
"recast": "^0.17.3"
46+
"recast": "^0.17.6"
4747
},
4848
"devDependencies": {
4949
"@babel/cli": "^7.0.0",
50-
"@babel/core": "^7.0.0",
5150
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
5251
"@babel/plugin-transform-runtime": "^7.0.0",
5352
"@babel/preset-env": "^7.0.0",

0 commit comments

Comments
 (0)