Skip to content

Commit a08ec49

Browse files
committed
ts 未完待续
1 parent 38c6814 commit a08ec49

File tree

13 files changed

+138
-97
lines changed

13 files changed

+138
-97
lines changed

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
},
2626
"dependencies": {
2727
"@rematch/core": "^1.2.0",
28+
"@types/axios": "^0.14.0",
29+
"@types/react": "^16.9.5",
30+
"@types/react-dom": "^16.9.1",
2831
"antd": "^3.23.6",
2932
"axios": "^0.19.0",
3033
"core-js": "^3.2.1",
@@ -39,18 +42,20 @@
3942
"redux": "^4.0.4"
4043
},
4144
"devDependencies": {
42-
"@babel/core": "^7.6.2",
45+
"@babel/core": "^7.6.3",
4346
"@babel/plugin-proposal-class-properties": "^7.5.5",
4447
"@babel/plugin-proposal-decorators": "^7.6.0",
4548
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
4649
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
4750
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
4851
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
4952
"@babel/plugin-transform-runtime": "^7.6.2",
50-
"@babel/preset-env": "^7.6.2",
51-
"@babel/preset-react": "^7.0.0",
52-
"@babel/runtime": "^7.6.2",
53+
"@babel/preset-env": "^7.6.3",
54+
"@babel/preset-react": "^7.6.3",
55+
"@babel/runtime": "^7.6.3",
56+
"@types/react-redux": "^7.1.4",
5357
"autoprefixer": "^9.6.4",
58+
"awesome-typescript-loader": "^5.2.1",
5459
"babel-eslint": "^10.0.3",
5560
"babel-loader": "^8.0.6",
5661
"babel-plugin-import": "^1.12.2",
@@ -75,9 +80,11 @@
7580
"optimize-css-assets-webpack-plugin": "^5.0.3",
7681
"postcss-loader": "^3.0.0",
7782
"prettier": "1.18.2",
83+
"source-map-loader": "^0.2.4",
7884
"style-loader": "^1.0.0",
7985
"sw-precache-webpack-plugin": "^0.11.5",
8086
"terser-webpack-plugin": "^2.1.2",
87+
"typescript": "^3.6.3",
8188
"url-loader": "^2.2.0",
8289
"webpack": "^4.41.0",
8390
"webpack-cli": "^3.3.9",

src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const axios: any;

src/index.js renamed to src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ ReactDOM.render(<Root />, document.getElementById("app-root"));
1515

1616
serviceWorker.register();
1717

18-
if (module.hot) {
19-
module.hot.accept();
18+
if ((module as any).hot) {
19+
(module as any).hot.accept();
2020
}
File renamed without changes.

src/root/index.js renamed to src/root/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { Provider } from "react-redux";
1010
import store from "../store";
1111
import Routers from "../container/routers";
1212

13-
export default function RootContainer() {
13+
const RootContainer: React.FC = () => {
1414
return (
1515
<Provider store={store}>
1616
<Routers />
1717
</Provider>
1818
);
19-
}
19+
};
20+
21+
export default RootContainer;

src/serviceWorker.js renamed to src/serviceWorker.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@
88
// resources are updated in the background.
99

1010
// To learn more about the benefits of this model and instructions on how to
11-
// opt-in, read http://bit.ly/CRA-PWA
11+
// opt-in, read https://bit.ly/CRA-PWA
1212

1313
const isLocalhost = Boolean(
14-
window.location.hostname === "localhost" ||
14+
window.location.hostname === 'localhost' ||
1515
// [::1] is the IPv6 localhost address.
16-
window.location.hostname === "[::1]" ||
16+
window.location.hostname === '[::1]' ||
1717
// 127.0.0.1/8 is considered localhost for IPv4.
1818
window.location.hostname.match(
1919
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
2020
)
2121
);
2222

23-
export function register(config) {
24-
if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {
23+
type Config = {
24+
onSuccess?: (registration: ServiceWorkerRegistration) => void;
25+
onUpdate?: (registration: ServiceWorkerRegistration) => void;
26+
};
27+
28+
export function register(config?: Config) {
29+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
2530
// The URL constructor is available in all browsers that support SW.
26-
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
31+
const publicUrl = new URL(
32+
(process as { env: { [key: string]: string } }).env.PUBLIC_URL,
33+
window.location.href
34+
);
2735
if (publicUrl.origin !== window.location.origin) {
2836
// Our service worker won't work if PUBLIC_URL is on a different origin
2937
// from what our page is served on. This might happen if a CDN is used to
3038
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
3139
return;
3240
}
3341

34-
window.addEventListener("load", () => {
42+
window.addEventListener('load', () => {
3543
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
3644

3745
if (isLocalhost) {
@@ -42,8 +50,8 @@ export function register(config) {
4250
// service worker/PWA documentation.
4351
navigator.serviceWorker.ready.then(() => {
4452
console.log(
45-
"This web app is being served cache-first by a service " +
46-
"worker. To learn more, visit http://bit.ly/CRA-PWA"
53+
'This web app is being served cache-first by a service ' +
54+
'worker. To learn more, visit https://bit.ly/CRA-PWA'
4755
);
4856
});
4957
} else {
@@ -54,7 +62,7 @@ export function register(config) {
5462
}
5563
}
5664

57-
function registerValidSW(swUrl, config) {
65+
function registerValidSW(swUrl: string, config?: Config) {
5866
navigator.serviceWorker
5967
.register(swUrl)
6068
.then(registration => {
@@ -64,14 +72,14 @@ function registerValidSW(swUrl, config) {
6472
return;
6573
}
6674
installingWorker.onstatechange = () => {
67-
if (installingWorker.state === "installed") {
75+
if (installingWorker.state === 'installed') {
6876
if (navigator.serviceWorker.controller) {
6977
// At this point, the updated precached content has been fetched,
7078
// but the previous service worker will still serve the older
7179
// content until all client tabs are closed.
7280
console.log(
73-
"New content is available and will be used when all " +
74-
"tabs for this page are closed. See http://bit.ly/CRA-PWA."
81+
'New content is available and will be used when all ' +
82+
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
7583
);
7684

7785
// Execute callback
@@ -82,7 +90,7 @@ function registerValidSW(swUrl, config) {
8290
// At this point, everything has been precached.
8391
// It's the perfect time to display a
8492
// "Content is cached for offline use." message.
85-
console.log("Content is cached for offline use.");
93+
console.log('Content is cached for offline use.');
8694

8795
// Execute callback
8896
if (config && config.onSuccess) {
@@ -94,19 +102,19 @@ function registerValidSW(swUrl, config) {
94102
};
95103
})
96104
.catch(error => {
97-
console.error("Error during service worker registration:", error);
105+
console.error('Error during service worker registration:', error);
98106
});
99107
}
100108

101-
function checkValidServiceWorker(swUrl, config) {
109+
function checkValidServiceWorker(swUrl: string, config?: Config) {
102110
// Check if the service worker can be found. If it can't reload the page.
103111
fetch(swUrl)
104112
.then(response => {
105113
// Ensure service worker exists, and that we really are getting a JS file.
106-
const contentType = response.headers.get("content-type");
114+
const contentType = response.headers.get('content-type');
107115
if (
108116
response.status === 404 ||
109-
(contentType != null && contentType.indexOf("javascript") === -1)
117+
(contentType != null && contentType.indexOf('javascript') === -1)
110118
) {
111119
// No service worker found. Probably a different app. Reload the page.
112120
navigator.serviceWorker.ready.then(registration => {
@@ -121,13 +129,13 @@ function checkValidServiceWorker(swUrl, config) {
121129
})
122130
.catch(() => {
123131
console.log(
124-
"No internet connection found. App is running in offline mode."
132+
'No internet connection found. App is running in offline mode.'
125133
);
126134
});
127135
}
128136

129137
export function unregister() {
130-
if ("serviceWorker" in navigator) {
138+
if ('serviceWorker' in navigator) {
131139
navigator.serviceWorker.ready.then(registration => {
132140
registration.unregister();
133141
});
File renamed without changes.

src/util/server.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/util/server.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 自己封装的异步请求函数
3+
* APP中的所有请求都将汇聚于此
4+
* **/
5+
6+
import axios, { Method } from "axios";
7+
8+
export default class ApiService {
9+
static newServer(url: string, bodyObj: object = {}, method: Method = "post") {
10+
return axios({
11+
url,
12+
method,
13+
headers: {
14+
"Content-Type": "application/json;charset=utf-8",
15+
},
16+
withCredentials: true,
17+
data: JSON.stringify(bodyObj),
18+
});
19+
}
20+
}

src/util/tools.js renamed to src/util/tools.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const tools = {
99
str - 数字或字符串
1010
x - 保留几位小数点
1111
*/
12-
pointX(str, x = 0) {
12+
pointX(str: string | number, x: number = 0) {
1313
if (!str && str !== 0) {
1414
return str;
1515
}
@@ -23,7 +23,7 @@ const tools = {
2323
/**
2424
去掉字符串两端空格
2525
*/
26-
trim(str) {
26+
trim(str: string) {
2727
const reg = /^\s*|\s*$/g;
2828
return str.replace(reg, "");
2929
},
@@ -33,7 +33,7 @@ const tools = {
3333
如:将123456转换为1****6,最多将字符串中间6个字符变成*
3434
如果字符串长度小于等于2,将不会有效果
3535
*/
36-
addMosaic(str) {
36+
addMosaic(str: string | number) {
3737
const s = String(str);
3838
const lenth = s.length;
3939
const howmuch = (() => {
@@ -59,7 +59,7 @@ const tools = {
5959
字符串加密
6060
简单的加密方法
6161
*/
62-
compile(code) {
62+
compile(code: string) {
6363
let c = String.fromCharCode(code.charCodeAt(0) + code.length);
6464
for (let i = 1; i < code.length; i++) {
6565
c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
@@ -71,14 +71,14 @@ const tools = {
7171
字符串解谜
7272
对应上面的字符串加密方法
7373
*/
74-
uncompile(code) {
74+
uncompile(code: string) {
7575
let c = String.fromCharCode(code.charCodeAt(0) - code.length);
7676
for (let i = 1; i < code.length; i++) {
7777
c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
7878
}
7979
console.log("解谜:", code, c);
8080
return c;
81-
}
81+
},
8282
};
8383

8484
export default tools;

0 commit comments

Comments
 (0)