40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
export const tryInt = (s: string): number | null => {
|
|
try {
|
|
return parseInt(s);
|
|
} catch (e) {
|
|
console.error(e);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const tryMaybeInt = (maybeString: string | null): number | null => {
|
|
if (maybeString) {
|
|
try {
|
|
return parseInt(maybeString);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
export const hasValue = <T>(value?: T | null): value is T => {
|
|
return value !== undefined && value !== null;
|
|
};
|
|
|
|
export type WithStringDates<T> = {
|
|
[K in keyof T]: T[K] extends Date
|
|
? string
|
|
: T[K] extends Date | null | undefined
|
|
? string | null | undefined
|
|
: T[K] extends Record<string, any>
|
|
? WithStringDates<T[K]>
|
|
: T[K] extends Record<string, any> | null | undefined
|
|
? WithStringDates<T[K]> | null | undefined
|
|
: T[K];
|
|
};
|
|
|
|
export const notNullOrUndefined = <T>(t: T | null | undefined): t is T => {
|
|
return t !== null && t !== undefined;
|
|
};
|