Skip to content

Commit

Permalink
Add use in TypeScript instruction (ant-design#8230)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck authored Nov 19, 2017
1 parent cd67045 commit 2de0a6a
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Properties for row selection.
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| getCheckboxProps | Get Checkbox or Radio props | Function(record) | - |
| fixed | Fixed selection column on the left | boolean | - |
| hideDefaultSelections | Remove the default `Select All` and `Select Invert` selections | boolean | `false` |
| selectedRowKeys | Controlled selected row keys | string\[] | \[] |
| selections | Custom selection [config](#rowSelection), only displays default selections when set to `true` | object\[]\|boolean | - |
Expand Down
1 change: 1 addition & 0 deletions components/table/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const columns = [{
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| getCheckboxProps | 选择框的默认属性配置 | Function(record) | - |
| fixed | 把选择框列固定在左边 | boolean | - |
| hideDefaultSelections | 去掉『全选』『反选』两个默认选项 | boolean | false |
| selectedRowKeys | 指定选中项的 key 数组,需要和 onChange 进行配合 | string\[] | \[] |
| selections | 自定义选择项 [配置项](#selection), 设为 `true` 时使用默认选择项 | object\[]\|boolean | true |
Expand Down
240 changes: 240 additions & 0 deletions docs/react/use-in-typescript.en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
---
order: 5
title: Use in TypeScript
---

Let's create a TypeScript project by using `create-react-app`, then import `antd` step by step.

---

## Install and Initialization

Ensure your system has installed latest version of [yarn](https://yarnpkg.com) or [npm](https://www.npmjs.com/).

Create a new project named `antd-demo-ts` using yarn.

```bash
$ yarn create react-app antd-demo-ts --scripts-version=react-scripts-ts
```

If you are using npm (we will use yarn in the following instructions, it's ok to replace yarn with npm)

```bash
$ npm install -g create-react-app
$ create-react-app antd-demo-ts --scripts-version=react-scripts-ts
```

Then we go inside `antd-demo-ts` and start it.

```bash
$ cd antd-demo-ts
$ yarn start
```

Open browser at http://localhost:3000/, it renders a header saying "Welcome to React" on the page.

## Import antd

```bash
$ yarn add antd
```

Modify `src/App.js`, import Button component from `antd`.

```jsx
import * as React from 'react';
import Button from 'antd/lib/button';
import './App.css';

class App extends React.Component {
render() {
return (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
}
}

export default App;
```

Add `antd/dist/antd.css` at the top of `src/App.css`.

```css
@import '~antd/dist/antd.css';

.App {
text-align: center;
}

...
```

Now you may see following error on the browser, this is because there are many implicitly `any` types in antd's codebase ([please help us improving type definition](https://github.com/ant-design/ant-design/issues/5627).

> error TS7006: Parameter 'e' implicitly has an 'any' type.

You need setting `skipLibCheck` to `true` in tsconfig.json, then TypeScript will skip type checking for third party libraries.

```json
{
"compilerOptions": {
...,
"skipLibCheck": true
}
}
```

Ok, reboot with `yarn start`, you should now see a blue primary button displayed on the page. Next you can choose any components of `antd` to develop your application. Visit other workflows of `create-react-app` at its [User Guide ](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md).

## Advanced Guides

We are successfully running antd components now but in the real world, there are still lots of problems about antd-demo-ts.
For instance, we actually import all styles of components in the project which may be a network performance issue.

Now we need to customize the default webpack config. We can achieve that by using [react-app-rewired](https://github.com/timarney/react-app-rewired) which is one of create-react-app's custom config solutions.

Import react-app-rewired and modify the `scripts` field in package.json.

```
$ yarn add react-app-rewired --dev
```


```diff
/* package.json */
"scripts": {
- "start": "react-scripts start",
+ "start": "react-app-rewired start --scripts-version react-scripts-ts",
- "build": "react-scripts build",
+ "build": "react-app-rewired build --scripts-version react-scripts-ts",
- "test": "react-scripts test --env=jsdom",
+ "test": "react-app-rewired test --env=jsdom --scripts-version react-scripts-ts",
}
```

Then create a `config-overrides.js` at root directory of your project for further overriding.

```js
module.exports = function override(config, env) {
// do stuff with the webpack config...
return config;
};
```

### Use ts-import-plugin

[ts-import-plugin](https://github.com/Brooooooklyn/ts-import-plugin) is a TypeScript plugin for importing components on demand ([How does it work?](/docs/react/getting-started#Import-on-Demand)). We are now trying to install it and modify `config-overrides.js`.

```bash
$ yarn add ts-import-plugin --dev
```

```js
/* config-overrides.js */
const tsImportPluginFactory = require('ts-import-plugin')
const { getLoader } = require("react-app-rewired");

module.exports = function override(config, env) {
const tsLoader = getLoader(
config.module.rules,
rule =>
rule.loader &&
typeof rule.loader === 'string' &&
rule.loader.includes('ts-loader')
);

tsLoader.options = {
getCustomTransformers: () => ({
before: [ tsImportPluginFactory({
libraryName: 'antd',
libraryDirectory: 'es',
style: 'css',
}) ]
})
};

return config;
}
```

Remove the `@import '~antd/dist/antd.css';` statement added before because `babel-plugin-import` will import styles and import components like below:

```diff
import * as React from 'react';
import { Button } from 'antd';
import './App.css';

class App extends React.Component {
render() {
return (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
}
}

export default App;
```

Then reboot with `yarn start` and visit the demo page, you should not find any [warning messages](https://zos.alipayobjects.com/rmsportal/vgcHJRVZFmPjAawwVoXK.png) in the console, which prove that the `import on demand` config is working now. You will find more info about it in [this guide](/docs/react/getting-started#Import-on-Demand).

### Customize Theme

According to the [Customize Theme documentation](/docs/react/customize-theme), to customize the theme, we need to modify `less` variables with tools such as [less-loader](https://github.com/webpack/less-loader). We can also use [react-app-rewire-less](http://npmjs.com/react-app-rewire-less) to achieve this. Import it and modify `config-overrides.js` like below.

```bash
$ yarn add react-app-rewire-less --dev
```

```diff
const tsImportPluginFactory = require('ts-import-plugin')
const { getLoader } = require("react-app-rewired");
+ const rewireLess = require('react-app-rewire-less');

module.exports = function override(config) {
const tsLoader = getLoader(
config.module.rules,
rule =>
rule.loader &&
typeof rule.loader === 'string' &&
rule.loader.includes('ts-loader')
);

tsLoader.options = {
getCustomTransformers: () => ({
before: [ tsImportPluginFactory({
libraryName: 'antd',
libraryDirectory: 'es',
- style: 'css',
+ style: true,
}) ]
})
};

+ config = rewireLess.withLoaderOptions({
+ modifyVars: { "@primary-color": "#1DA57A" },
+ })(config, env);

return config;
}
```

We use `modifyVars` option of [less-loader](https://github.com/webpack/less-loader#less-options) here, you can see a green button rendered on the page after rebooting the start server.

## Alternative way

You can also follow instructions in [Use in create-react-app](/docs/react/use-with-create-react-app.en-US.md), then use [react-app-rewire-typescript][https://github.com/lwd-technology/react-app-rewire-typescript] to setup the TypeScript development environment by yourself.

## FAQ

### error TS7006: Parameter 'x' implicitly has an 'any' type.

You need setting `skipLibCheck` to `true` in tsconfig.json.

### error TS2605: JSX element type Xxx is not a constructor function for JSX elements.

Before antd 3, You need setting `allowSyntheticDefaultImports` to `true` in tsconfig.json.

Loading

0 comments on commit 2de0a6a

Please sign in to comment.