在使用 create-react-app 時医寿,指定了語言為 TypeScript:
create-react-app charley-react-typescript --scripts-version=react-scripts-ts
在使用 Antd UI庫時毒返,引入相關(guān)的組件:
import * as React from 'react';
import './assets/css/main.css';
import Button from 'antd/lib/button';
class App extends React.Component {
public render() {
return (
<div className='App'>
<Button type='primary'>Button</Button>
</div>
);
}
}
export default App;
main.css 內(nèi)容如下:
@import '~antd/dist/antd.css';
運行 yarn start
劝萤,打開瀏覽器咐鹤,發(fā)現(xiàn)報了一個錯誤:
Import sources within a group must be alphabetized.
這是由于 tslint 規(guī)定了必須按順序?qū)胛募性鳎采w這個規(guī)則先朦,需要在根目錄的的 tslint.json 中增加以下配置:
{
"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
+ "rules": {
+ "ordered-imports": false
+ },
"linterOptions": {
"exclude": [
"config/**/*.js",
"node_modules/**/*.ts"
]
}
}
然后重啟服務(wù)颂跨,運行 yarn start
耙册,就不會再報這個錯誤了。
完毫捣。