Fix: fix typescript errors

This commit is contained in:
Nelson Chan 2021-08-23 11:33:24 +08:00
parent 2912ca1248
commit ee3bf2961c
2 changed files with 18 additions and 13 deletions

View File

@ -1,4 +1,3 @@
// @ts-nocheck
// Common Util for frontend and backend // Common Util for frontend and backend
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
@ -13,7 +12,7 @@ export const DOWN = 0;
export const UP = 1; export const UP = 1;
export const PENDING = 2; export const PENDING = 2;
export function flipStatus(s) { export function flipStatus(s: number) {
if (s === UP) { if (s === UP) {
return DOWN; return DOWN;
} }
@ -25,7 +24,7 @@ export function flipStatus(s) {
return s; return s;
} }
export function sleep(ms) { export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
@ -33,8 +32,8 @@ export function sleep(ms) {
* PHP's ucfirst * PHP's ucfirst
* @param str * @param str
*/ */
export function ucfirst(str) { export function ucfirst(str: string) {
if (! str) { if (!str) {
return str; return str;
} }
@ -42,12 +41,15 @@ export function ucfirst(str) {
return firstLetter.toUpperCase() + str.substr(1); return firstLetter.toUpperCase() + str.substr(1);
} }
export function debug(msg) { export function debug(msg: any) {
if (isDev) { if (isDev) {
console.log(msg); console.log(msg);
} }
} }
declare global { interface String { replaceAll(str: string, newStr: string): string; } }
export function polyfill() { export function polyfill() {
/** /**
* String.prototype.replaceAll() polyfill * String.prototype.replaceAll() polyfill
@ -56,7 +58,7 @@ export function polyfill() {
* @license MIT * @license MIT
*/ */
if (!String.prototype.replaceAll) { if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (str, newStr) { String.prototype.replaceAll = function (str: string, newStr: string) {
// If a regex pattern // If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") {
@ -71,11 +73,13 @@ export function polyfill() {
} }
export class TimeLogger { export class TimeLogger {
startTime: number;
constructor() { constructor() {
this.startTime = dayjs().valueOf(); this.startTime = dayjs().valueOf();
} }
print(name) { print(name: string) {
if (isDev) { if (isDev) {
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms") console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms")
} }
@ -85,7 +89,7 @@ export class TimeLogger {
/** /**
* Returns a random number between min (inclusive) and max (exclusive) * Returns a random number between min (inclusive) and max (exclusive)
*/ */
export function getRandomArbitrary(min, max) { export function getRandomArbitrary(min: number, max: number) {
return Math.random() * (max - min) + min; return Math.random() * (max - min) + min;
} }
@ -98,7 +102,7 @@ export function getRandomArbitrary(min, max) {
* lower than max if max isn't an integer). * lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution! * Using Math.round() will give you a non-uniform distribution!
*/ */
export function getRandomInt(min, max) { export function getRandomInt(min: number, max: number) {
min = Math.ceil(min); min = Math.ceil(min);
max = Math.floor(max); max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min;

View File

@ -4,14 +4,15 @@
"target": "es2018", "target": "es2018",
"module": "commonjs", "module": "commonjs",
"lib": [ "lib": [
"es2020" "es2020",
"DOM",
], ],
"removeComments": true, "removeComments": true,
"preserveConstEnums": true, "preserveConstEnums": true,
"sourceMap": false, "sourceMap": false,
"files.insertFinalNewline": true "strict": true
}, },
"files": [ "files": [
"./server/util.ts" "./src/util.ts"
] ]
} }