chore: Improve linting within @k8slens/core

- Turning on @typescript-eslint/recommended-requiring-type-checking
- Turning off @typescript-eslint/no-unnecessary-type-assertion (due too many false positives)
- Making @typescript-eslint/no-explicit-any an error (except in tests)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-06-01 09:17:17 -04:00
1 parent 6184c2f03b
commit fa44b795d4
734 files changed
+32216 -4566

No files matched your search

@@ -1,4 +1,5 @@
export * from "./src/abort-controller";
export * from "./src/add-event-listener";
export * from "./src/array";
export * from "./src/backoff-caller";
export * from "./src/base64";
@@ -0,0 +1,16 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export function addEventListener<K extends keyof HTMLElementEventMap>(target: HTMLElement, type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions) {
target.addEventListener(type, listener, options);
return () => target.removeEventListener(type, listener, options);
}
export function addWindowEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions) {
window.addEventListener(type, listener, options);
return () => window.removeEventListener(type, listener, options);
}
@@ -12,6 +12,15 @@ function filled<T>(length: number, value: T): T[] {
return Array(length).fill(value);
}
/**
* A inference typed version of `Array(length).fill(value)`
* @param length The number of entries
* @param value The value of each of the indices
*/
function filledWith<T>(length: number, fn: (index: number) => T): T[] {
return Array(length).fill(undefined).map((value, index) => fn(index));
}
/**
* This function splits an array into two sub arrays on the first instance of
* element (from the left). If the array does not contain the element. The
@@ -52,4 +61,5 @@ export const array = {
bifurcate,
filled,
split,
filledWith,
};
@@ -56,7 +56,7 @@ export const backoffCaller = async <T, E>(fn: () => AsyncResult<T, E>, options?:
do {
result = await fn();
if (result.callWasSuccessful) {
if (result.isOk) {
return result;
}
@@ -3,20 +3,16 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Result } from "./result";
import type { JsonValue } from "type-fest";
import { result, Result } from "./result";
export const json = {
parse: (input: string): Result<unknown, Error> => {
try {
return {
callWasSuccessful: true,
response: JSON.parse(input) as unknown,
}
return result.ok(JSON.parse(input) as unknown);
} catch (error) {
return {
callWasSuccessful: false,
error: error as Error,
}
return result.error(error as Error);
}
},
clone: <T>(input: JsonValue & T): T => JSON.parse(JSON.stringify(input)),
};
@@ -17,12 +17,12 @@ import type React from "react";
// <Icon onClick={prevDefault(() => console.log('stay on the page and open dialog'))}/>
// </a>
export function prevDefault<E extends React.SyntheticEvent | Event, R>(callback: (evt: E) => R): (event: E) => R {
export function prevDefault<E extends React.SyntheticEvent | Event>(callback: (evt: E) => unknown): (event: E) => void {
return function (evt: E) {
evt.preventDefault();
evt.stopPropagation();
return callback(evt);
return void callback(evt);
};
}
@@ -6,16 +6,34 @@
/**
* A helper type for async functions that return a `Result`
*/
export type AsyncResult<Response, Error = string> = Promise<Result<Response, Error>>;
export type AsyncResult<Value, Err> = Promise<Result<Value, Err>>;
/**
* Result describes the "error is just more data" pattern instead of using exceptions for
* normal execution
*/
export type Result<Response, Error = string> =
| (
Response extends void
? { callWasSuccessful: true; response?: undefined }
: { callWasSuccessful: true; response: Response }
)
| { callWasSuccessful: false; error: Error };
export type Result<Value, Err> =
| OkResult<Value>
| ErrorResult<Err>;
export type ErrorResult<Err> = { readonly isOk: false; readonly error: Err };
export type OkResult<Value> = (
Value extends void
? { readonly isOk: true; readonly value?: undefined }
: { readonly isOk: true; readonly value: Value }
);
export const result = {
ok: <Value>(value: Value) => ({
isOk: true,
value: value,
}) as OkResult<Value>,
error: <Err>(error: Err) => ({
isOk: false,
error,
}) as ErrorResult<Err>,
wrapError: <Err>(error: string, cause: ErrorResult<Err>) => ({
isOk: false,
error: new Error(error, { cause: cause.error }),
}) as ErrorResult<Error>,
}
@@ -123,10 +123,17 @@ export function isDefined<T>(val: T | undefined | null): val is T {
return val != null;
}
export function isFunction(val: unknown): val is (...args: unknown[]) => unknown {
export type ExtractOr<T, U, Fallback = never> = Extract<T, U> extends never ? Fallback : Extract<T, U>;
// @ts-expect-error
export declare function isFunction<T>(val: T): val is ExtractOr<T, Function, (...args: unknown[]) => unknown> {
return typeof val === "function";
}
export function isArray(val: unknown): val is unknown[] {
return Array.isArray(val);
}
/**
* Checks if the value in the second position is non-nullable
*/
@@ -196,14 +203,14 @@ export type ComputeOutputFormat<Format> = Format extends "string"
? Buffer
: string | Buffer;
export interface ChildProcessExecpetion<Format> extends ExecFileException {
export interface ChildProcessException<Format> extends ExecFileException {
stderr: ComputeOutputFormat<Format>;
stdout: ComputeOutputFormat<Format>;
}
const isStringOrBuffer = (val: unknown): val is string | Buffer => isString(val) || isBuffer(val);
export function isChildProcessError(error: unknown, format?: OutputFormat): error is ChildProcessExecpetion<typeof format> {
export function isChildProcessError(error: unknown, format?: OutputFormat): error is ChildProcessException<typeof format> {
if (!isExecFileException(error)) {
return false;
}