2017-11-19 22:00:44 +08:00
---
2019-12-17 22:25:28 +08:00
order: 5
2017-11-19 22:00:44 +08:00
title: Use in TypeScript
---
Let's create a TypeScript project by using `create-react-app` , then import `antd` step by step.
2021-02-25 11:11:43 +08:00
> We build `antd` based on latest stable version of TypeScript (`>=4.0.0`), please make sure your project dependency matches it.
2020-04-02 12:06:45 +08:00
2017-11-19 22:00:44 +08:00
---
## Install and Initialization
Ensure your system has installed latest version of [yarn ](https://yarnpkg.com ) or [npm ](https://www.npmjs.com/ ).
2019-12-06 12:25:27 +08:00
Create a new [cra-template-typescript ](https://github.com/facebook/create-react-app/tree/master/packages/cra-template-typescript ) project named `antd-demo-ts` using yarn.
2017-11-19 22:00:44 +08:00
```bash
2019-12-06 12:25:27 +08:00
$ yarn create react-app antd-demo-ts --template typescript
2017-11-19 22:00:44 +08:00
```
If you are using npm (we will use yarn in the following instructions, it's ok to replace yarn with npm)
```bash
2021-06-11 08:34:16 +08:00
$ npx create-react-app antd-demo-ts --template typescript
2017-11-19 22:00:44 +08:00
```
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
```
2018-06-16 17:49:15 +08:00
Modify `src/App.tsx` , import Button component from `antd` .
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
```tsx
2023-01-11 16:48:09 +08:00
import React from 'react';
import type { FC } from 'react';
2020-05-15 17:18:55 +08:00
import { Button } from 'antd';
2022-11-18 14:23:50 +08:00
import 'antd/dist/reset.css';
2017-11-19 22:00:44 +08:00
import './App.css';
2020-05-15 17:18:55 +08:00
const App: FC = () => (
< div className = "App" >
< Button type = "primary" > Button< / Button >
< / div >
);
2017-11-19 22:00:44 +08:00
export default App;
```
2021-09-23 22:13:05 +08:00
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 it's [User Guide ](https://create-react-app.dev/docs/getting-started#creating-a-typescript-app ).
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
`antd` is written in TypeScript with complete definitions, try out and enjoy the property suggestion and typing check.
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
![](https://gw.alipayobjects.com/zos/antfincdn/26L5vPoLug/8d7da796-175e-40af-8eea-e7031ba09f9f.png)
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
> Don't install `@types/antd`.
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
## Advanced Guides
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
In the real world, we usually have to modify default webpack config for custom needs such as themes. We can achieve that by using [craco ](https://github.com/gsoft-inc/craco ) which is one of create-react-app's custom config solutions.
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
Install craco and modify the `scripts` field in `package.json` .
2017-11-19 22:00:44 +08:00
2020-05-15 17:18:55 +08:00
```bash
$ yarn add @craco/craco
2017-11-19 22:00:44 +08:00
```
```diff
/* package.json */
"scripts": {
2019-01-30 15:03:11 +08:00
- "start": "react-scripts start",
- "build": "react-scripts build",
- "test": "react-scripts test",
2020-05-15 17:18:55 +08:00
+ "start": "craco start",
+ "build": "craco build",
+ "test": "craco test",
2017-11-19 22:00:44 +08:00
}
```
2020-05-15 17:18:55 +08:00
Then create a `craco.config.js` at root directory of your project for further overriding.
2017-11-19 22:00:44 +08:00
```js
2020-05-15 17:18:55 +08:00
/* craco.config.js */
module.exports = {
// ...
2017-11-19 22:00:44 +08:00
};
```
2020-05-15 17:18:55 +08:00
### Customize Theme
2017-11-19 22:00:44 +08:00
2022-11-24 11:26:38 +08:00
Ref to the [Customize Theme documentation ](/docs/react/customize-theme ). Modify theme with ConfigProvider:
2017-11-19 22:00:44 +08:00
2022-11-16 23:38:38 +08:00
```tsx
import React from 'react';
import { ConfigProvider } from 'antd';
export default () => (
< ConfigProvider
theme={{
token: {
colorPrimary: '#00b96b',
2020-05-15 17:18:55 +08:00
},
2022-11-16 23:38:38 +08:00
}}
>
< MyApp / >
< / ConfigProvider >
);
2017-11-19 22:00:44 +08:00
```
2020-05-15 17:18:55 +08:00
## Alternative ways
2019-01-31 17:17:24 +08:00
2020-05-15 17:18:55 +08:00
Follow manual in [Adding TypeScript ](https://create-react-app.dev/docs/adding-typescript ) to setup TypeScript development environment if you already create a project by [Use in create-react-app ](/docs/react/use-with-create-react-app ).
2019-01-31 17:17:24 +08:00
2019-01-31 17:23:49 +08:00
- [Create React apps (with Typescript and antd) with no build configuration ](https://github.com/SZzzzz/react-scripts-ts-antd )
2020-04-21 23:40:41 +08:00
- [react-app-rewire-typescript ](https://github.com/lwd-technology/react-app-rewire-typescript )
2019-01-31 17:23:49 +08:00
- [ts-import-plugin ](https://github.com/Brooooooklyn/ts-import-plugin )
2019-01-31 17:17:24 +08:00
- [Migrating from create-react-app-typescript to Create React App ](https://vincenttunru.com/migrate-create-react-app-typescript-to-create-react-app/ )