| 版本号 | 描述 | |
|---|---|---|
| node -v | v14.20.0 | |
| npm -v | 6.14.6 | |
| tsc -v | 4.7.4 | |
| ts-node -v | 10.9.1 |
参考文章:【TypeScript】使用CRA创建支持TS的React项目(从踩坑到放弃) https://blog.csdn.net/kinghzking/article/details/126188004
核心命令:npx create-react-app react-ts-basic --template typescript。
相比非TS项目的三个变化
https://www.typescriptlang.org/tsconfig

示例代码如下所示:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
// import App from './App';
import reportWebVitals from './reportWebVitals';
type Props = {name: string, age?: number}
const Hello: React.FC<Props> = ({name, age}) => {
return <div>
您好,我叫:{name},我{age}岁了。
</div>;
}
const App = () => {
return <div>
<Hello name='khz' age={12} />
</div>
}
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(<App />);


示例代码:



@types/*库的地址:https://www.typescriptlang.org/dt/search?search=