Browse Source

Feature/enable strict null checks in libs/common (#6250)

* feat(ts): enable strict null checks in libs/common

* feat(lint): enable prefer-nullish-coalescing

* feat(lib): resolve errors

* fix(lib): revert changes on DateRange type
pull/6249/head^2
Kenrick Tandrian 6 days ago
committed by GitHub
parent
commit
53be141460
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      libs/common/eslint.config.cjs
  2. 26
      libs/common/src/lib/chart-helper.ts
  3. 2
      libs/common/src/lib/class-transformer.ts
  4. 8
      libs/common/src/lib/helper.ts
  5. 2
      libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts
  6. 2
      libs/common/src/lib/routes/interfaces/internal-route.interface.ts
  7. 4
      libs/common/src/lib/utils/form.util.ts
  8. 2
      libs/common/src/lib/validators/is-currency-code.ts
  9. 3
      libs/common/tsconfig.json

4
libs/common/eslint.config.cjs

@ -18,7 +18,9 @@ module.exports = [
{
files: ['**/*.ts', '**/*.tsx'],
// Override or add rules here
rules: {}
rules: {
'@typescript-eslint/prefer-nullish-coalescing': 'error'
}
},
{
files: ['**/*.js', '**/*.jsx'],

26
libs/common/src/lib/chart-helper.ts

@ -1,4 +1,10 @@
import { Chart, TooltipPosition } from 'chart.js';
import type { ElementRef } from '@angular/core';
import type {
Chart,
ChartTypeRegistry,
Plugin,
TooltipPosition
} from 'chart.js';
import { format } from 'date-fns';
import {
@ -34,12 +40,12 @@ export function getTooltipOptions({
locale = getLocale(),
unit = ''
}: {
colorScheme?: ColorScheme;
colorScheme: ColorScheme;
currency?: string;
groupBy?: GroupBy;
locale?: string;
unit?: string;
} = {}) {
}) {
return {
backgroundColor: getBackgroundColor(colorScheme),
bodyColor: `rgb(${getTextColor(colorScheme)})`,
@ -47,7 +53,7 @@ export function getTooltipOptions({
borderColor: `rgba(${getTextColor(colorScheme)}, 0.1)`,
callbacks: {
label: (context) => {
let label = context.dataset.label || '';
let label = context.dataset.label ?? '';
if (label) {
label += ': ';
}
@ -98,10 +104,10 @@ export function getTooltipPositionerMapTop(
};
}
export function getVerticalHoverLinePlugin(
chartCanvas,
colorScheme?: ColorScheme
) {
export function getVerticalHoverLinePlugin<T extends keyof ChartTypeRegistry>(
chartCanvas: ElementRef,
colorScheme: ColorScheme
): Plugin<T> {
return {
afterDatasetsDraw: (chart, _, options) => {
const active = chart.getActiveElements();
@ -110,8 +116,8 @@ export function getVerticalHoverLinePlugin(
return;
}
const color = options.color || `rgb(${getTextColor(colorScheme)})`;
const width = options.width || 1;
const color = options.color ?? `rgb(${getTextColor(colorScheme)})`;
const width = options.width ?? 1;
const {
chartArea: { bottom, top }

2
libs/common/src/lib/class-transformer.ts

@ -16,7 +16,7 @@ export function transformToMapOfBig({
return mapOfBig;
}
export function transformToBig({ value }: { value: string }): Big {
export function transformToBig({ value }: { value: string }): Big | null {
if (value === null) {
return null;
}

8
libs/common/src/lib/helper.ts

@ -144,7 +144,7 @@ export function extractNumberFromString({
}: {
locale?: string;
value: string;
}): number {
}): number | undefined {
try {
// Remove non-numeric characters (excluding international formatting characters)
const numericValue = value.replace(/[^\d.,'’\s]/g, '');
@ -273,7 +273,7 @@ export function getNumberFormatDecimal(aLocale?: string) {
return formatObject.find((object) => {
return object.type === 'decimal';
}).value;
})?.value;
}
export function getNumberFormatGroup(aLocale = getLocale()) {
@ -283,7 +283,7 @@ export function getNumberFormatGroup(aLocale = getLocale()) {
return formatObject.find((object) => {
return object.type === 'group';
}).value;
})?.value;
}
export function getStartOfUtcDate(aDate: Date) {
@ -394,7 +394,7 @@ export function isRootCurrency(aCurrency: string) {
});
}
export function parseDate(date: string): Date {
export function parseDate(date: string): Date | undefined {
if (!date) {
return undefined;
}

2
libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts

@ -39,7 +39,7 @@ export interface PublicPortfolioResponse extends PublicPortfolioResponseV1 {
})[];
markets: {
[key in Market]: Pick<
PortfolioDetails['markets'][key],
NonNullable<PortfolioDetails['markets']>[key],
'id' | 'valueInPercentage'
>;
};

2
libs/common/src/lib/routes/interfaces/internal-route.interface.ts

@ -2,7 +2,7 @@ import { User } from '@ghostfolio/common/interfaces';
export interface InternalRoute {
excludeFromAssistant?: boolean | ((aUser: User) => boolean);
path: string;
path?: string;
routerLink: string[];
subRoutes?: Record<string, InternalRoute>;
title: string;

4
libs/common/src/lib/utils/form.util.ts

@ -29,7 +29,7 @@ export async function validateObjectForForm<T>({
if (formControl) {
formControl.setErrors({
validationError: Object.values(constraints)[0]
validationError: Object.values(constraints ?? {})[0]
});
}
@ -37,7 +37,7 @@ export async function validateObjectForForm<T>({
if (formControlInCustomCurrency) {
formControlInCustomCurrency.setErrors({
validationError: Object.values(constraints)[0]
validationError: Object.values(constraints ?? {})[0]
});
}
}

2
libs/common/src/lib/validators/is-currency-code.ts

@ -9,7 +9,7 @@ import {
import { isISO4217CurrencyCode } from 'class-validator';
export function IsCurrencyCode(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
return function (object: object, propertyName: string) {
registerDecorator({
propertyName,
constraints: [],

3
libs/common/tsconfig.json

@ -12,6 +12,7 @@
],
"compilerOptions": {
"module": "preserve",
"lib": ["dom", "es2022"]
"lib": ["dom", "es2022"],
"strictNullChecks": true
}
}

Loading…
Cancel
Save