String(d.getDate())
instead153 if (!date) return '';
154 const d = new Date(date),
155 month = '' + (d.getMonth() + 1),
156 day = '' + d.getDate(),157 year = d.getFullYear();
158
159 return [pad(day, 2), pad(month, 2), year].join('.');
String(d.getMonth() + 1)
instead152function formatDate(date: Date) {
153 if (!date) return '';
154 const d = new Date(date),
155 month = '' + (d.getMonth() + 1),156 day = '' + d.getDate(),
157 year = d.getFullYear();
158
String(num)
instead144);
145
146function pad(num: string, size: number): string {
147 let s = num + "";148 while (s.length < size) s = "0" + s;
149 return s;
150}
Prefer using explicit casts by calling Number
, Boolean
, or String
over using operators like +
, !!
or "" +
.
This is considered best practice as it improves readability.
const b = !!foo;
// The `+` operator does not change the value of its operand
// unless it's already a number.
let n = +foo;
n = 1 * foo;
const s = "" + foo;
const b = Boolean(foo);
const n = Number(foo);
const s = String(foo);