This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const remClasses = ( | |
{ classList }: { classList: DOMTokenList }, | |
...classes: string[] | |
) => classList.remove(...classes); | |
export const addClasses = ( | |
{ classList }: { classList: DOMTokenList }, | |
...classes: string[] | |
) => classList.add(...classes); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Indices<A> = Exclude<keyof A, keyof any[]>; | |
type valueAtIndexToNever<T extends any[], I extends number> = { | |
[ P in keyof T ] : P extends Indices<T> ? | |
P extends `\${I}` ? never : T[P] : | |
T[P] | |
} | |
type test1 = valueAtIndexToNever<[1,2,3],1>; //[1, never, 3]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@see https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887 | |
type swithcNever<T, A, B> = [T] extends [never] ? A : B; | |
//@see https://stackoverflow.com/a/63568058/11407695 | |
type Flatten<T extends any[]> = T extends [infer U, ...infer R] | |
? U extends any[] | |
? [...Flatten<U>, ...Flatten<R>] | |
: [U, ...Flatten<R>] | |
: []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Invert<T> = { [ P in keyof T as Extract<T[P], string | number | symbol> ] : P }; | |
type test1 = Invert<{ a: "ia", b: "ib", c: 1 }>; //{ ia: "a"; ib: "b"; 1: "c"; } | |
type DeepPath<T extends string[], U> = T extends [infer F, ...infer L] | |
? F extends string | |
? { [P in F]: L extends string[] ? DeepPath<L,U> : never } | |
: {} | |
: U; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if gulp CLI is not installed | |
npm i -g gulp-cli | |
# gulp package | |
npm i -D gulp @types/gulp | |
# TypeScript prerequisites | |
# ts-node is required for gulpfile.ts | |
npm i -D typescript ts-node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "0"; | |
type EveryOfType<A extends readonly any[], T> = keyof { | |
[P in Exclude<keyof A, keyof any[]> as A[P] extends T ? never : P]: P; | |
} extends never | |
? true | |
: false; | |
type SplitString< | |
T extends string, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# extracts NPM environment variable names | |
npm_val() { | |
local value=$(ENV | grep -e "npm_package_$1" | cut -d "=" -f 2) | |
echo $value | |
} | |
# splits a string on dashes and rejoins with first letter uppercased | |
pretty() { | |
IFS=\- | |
local output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type NestedObj = { | |
[x: string]: string | number | boolean | null | undefined | NestedObj; | |
}; | |
const lens = (obj: NestedObj, path: string) => { | |
const parts = path.split("."); | |
const [lastPart] = parts.slice(-1); | |
let current: NestedObj = obj; | |
for (const part of parts) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PrintStringUnion<T extends string, S extends string = "|", A extends string = ""> = { | |
[P in T]: [T] extends [P] ? `${A}${P}` : PrintStringUnion<Exclude<T, P>, S, `${A}${P}${S}`> | |
}[T]; | |
type TestUnion = "A" | "B" | "C" | "D" | "E"; | |
type U = PrintStringUnion<TestUnion>; | |
const test1: U = "A|B|C|D|E"; // OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DeepKeyof<T, A = never> = { [P in keyof T]: T[P] extends object ? P extends A ? P : DeepKeyof<T[P], A | keyof T> | P : P }[keyof T]; | |
type DeepOmit<T, U extends DeepKeyof<T>> = { | |
[P in keyof T as P extends U ? never : P]: T[P] extends object ? | |
keyof DeepOmit<T[P], Extract<U, DeepKeyof<T[P]>>> extends never ? | |
{} : | |
DeepOmit<T[P], Extract<U, DeepKeyof<T[P]>>> : | |
T[P] | |
}; |