From ee3bf2961cc57ff757e333c545b3bec6874a5865 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 23 Aug 2021 11:33:24 +0800 Subject: [PATCH] Fix: fix typescript errors --- src/util.ts | 24 ++++++++++++++---------- tsconfig.json | 7 ++++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/util.ts b/src/util.ts index 7d7b8e01d..3b96f7b94 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,3 @@ -// @ts-nocheck // Common Util for frontend and backend // Backend uses the compiled file util.js // Frontend uses util.ts @@ -13,7 +12,7 @@ export const DOWN = 0; export const UP = 1; export const PENDING = 2; -export function flipStatus(s) { +export function flipStatus(s: number) { if (s === UP) { return DOWN; } @@ -25,7 +24,7 @@ export function flipStatus(s) { return s; } -export function sleep(ms) { +export function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -33,8 +32,8 @@ export function sleep(ms) { * PHP's ucfirst * @param str */ -export function ucfirst(str) { - if (! str) { +export function ucfirst(str: string) { + if (!str) { return str; } @@ -42,12 +41,15 @@ export function ucfirst(str) { return firstLetter.toUpperCase() + str.substr(1); } -export function debug(msg) { +export function debug(msg: any) { if (isDev) { console.log(msg); } } + +declare global { interface String { replaceAll(str: string, newStr: string): string; } } + export function polyfill() { /** * String.prototype.replaceAll() polyfill @@ -56,7 +58,7 @@ export function polyfill() { * @license MIT */ if (!String.prototype.replaceAll) { - String.prototype.replaceAll = function (str, newStr) { + String.prototype.replaceAll = function (str: string, newStr: string) { // If a regex pattern if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { @@ -71,11 +73,13 @@ export function polyfill() { } export class TimeLogger { + startTime: number; + constructor() { this.startTime = dayjs().valueOf(); } - print(name) { + print(name: string) { if (isDev) { 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) */ -export function getRandomArbitrary(min, max) { +export function getRandomArbitrary(min: number, max: number) { return Math.random() * (max - min) + min; } @@ -98,7 +102,7 @@ export function getRandomArbitrary(min, max) { * lower than max if max isn't an integer). * 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); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; diff --git a/tsconfig.json b/tsconfig.json index eb23619e4..41db8c524 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,14 +4,15 @@ "target": "es2018", "module": "commonjs", "lib": [ - "es2020" + "es2020", + "DOM", ], "removeComments": true, "preserveConstEnums": true, "sourceMap": false, - "files.insertFinalNewline": true + "strict": true }, "files": [ - "./server/util.ts" + "./src/util.ts" ] }