Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript support #348

Merged
merged 12 commits into from
May 3, 2019
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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",
Copy link
Collaborator

@danez danez May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update @babel/core to ^7.4.4 so we ensure that typescript is fully supported.

Also noticed we have it in dependencies and dev dependencies, so I guess we can remove it from dev.

"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
Loading