Skip to content

Commit

Permalink
Added support for TypeScript, Flow interfaces and Flow generic types (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
devongovett authored and danez committed May 3, 2019
1 parent c9a1556 commit d1b07c4
Show file tree
Hide file tree
Showing 31 changed files with 2,185 additions and 124 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -239,15 +239,15 @@ we are getting this output:
}
```

## Flow Type support
## Flow and TypeScript support

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`.
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`.

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

For the following component
For the following component with Flow types

```js
import React, { Component } from 'react';
Expand Down Expand Up @@ -387,6 +387,7 @@ The structure of the JSON blob / JavaScript object is as follows:
["raw": string]
},
"flowType": <flowType>,
"tsType": <tsType>,
"required": boolean,
"description": string,
["defaultValue": {
Expand All @@ -406,9 +407,11 @@ The structure of the JSON blob / JavaScript object is as follows:
- `<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"`.
- `<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.
- `<flowType>`: If using flow type this property contains the parsed flow type as can be seen in the table above.
- `<tsType>`: If using TypeScript type this property contains the parsed TypeScript type as can be seen in the table above.

[react]: http://facebook.github.io/react/
[flow]: http://flowtype.org/
[typescript]: http://typescriptlang.org/
[recast]: https://github.com/benjamn/recast
[@babel/parser]: https://github.com/babel/babel/tree/master/packages/babel-parser
[classes]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -37,17 +37,16 @@
"author": "Felix Kling",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.0.0",
"@babel/core": "^7.4.4",
"@babel/runtime": "^7.0.0",
"async": "^2.1.4",
"commander": "^2.19.0",
"doctrine": "^3.0.0",
"node-dir": "^0.1.10",
"recast": "^0.17.3"
"recast": "^0.17.6"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
Expand Down

0 comments on commit d1b07c4

Please sign in to comment.