Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
computers

Myles Scolnick mscolnick

💭
computers
View GitHub Profile
@mscolnick
mscolnick / vite-cypress.js
Created Mar 11, 2022
Plugin js for vite + cypress
View vite-cypress.js
const path = require('path');
const { createServer } = require('vite');
module.exports = async (on, config) => {
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || 3000;
// Vite config
const viteConfig = {
configFile: path.resolve(__dirname, "../vite.config.ts"),
View fibonacci.ts
type _Fib<NUM extends any[]> = NUM['length'] extends 1 | 0
? NUM
: Add<_Fib<Minus1<NUM>>, _Fib<Minus2<NUM>>>;
type Fib<N extends number> = _Fib<ArrayOfSize<N>>['length'];
type F1 = Fib<1>; // 1
type F2 = Fib<2>; // 1
type F3 = Fib<3>; // 2
View arithmetic.ts
type Plus1<A extends any[]> = [true, ...A];
type Add<A extends any[], B extends any[]> = [...A, ...B];
type Minus1<A extends any[]> = A extends readonly [any?, ...infer U] ? U : [...A];
type Minus2<A extends any[]> = Minus1<Minus1<A>>;
// verify
type D = Plus1<A>; // [true, true, true, true]
type E = Minus1<A>; // [true, true]
type F = Minus2<A>; // [true]
View array.ts
// number -> array
type ArrayOfSize<N extends number> = N extends N ? (number extends N ? boolean[] : _ArrayOfSize<N, []>) : never;
type _ArrayOfSize<N extends number, R extends unknown[]> = R['length'] extends N ? R : _ArrayOfSize<N, [true, ...R]>;
// verify
type A = ArrayOfSize<3>; // [true, true, true]
type B = ArrayOfSize<1>; // [true]
type C = ArrayOfSize<0>; // []
View recursive-conditional-types.ts
// Awaiting promises
type Awaited<T> =
T extends null | undefined ? T :
T extends PromiseLike<infer U> ? Awaited<U> :
T;
type P1 = Awaited<Promise<string>>; // string
type P2 = Awaited<Promise<Promise<string>>>; // string
type P3 = Awaited<Promise<string | Promise<Promise<number> | undefined>>; // string | number | undefined
View variadic-tuple-types.ts
// Variadic tuple elements
type Foo<T extends unknown[]> = [string, ...T, number];
type T1 = Foo<[boolean]>; // [string, boolean, number]
type T2 = Foo<[number, number]>; // [string, number, number, number]
type T3 = Foo<[]>; // [string, number]
// Strongly typed tuple concatenation
View infer-with-conditional-types.ts
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
T extends Promise<infer U> ? U :
T;
type T0 = Unpacked<string>; // string
type T1 = Unpacked<string[]>; // string
type T2 = Unpacked<() => string>; // string
type T3 = Unpacked<Promise<string>>; // string
View conditional-types.ts
type TypeName<T> =
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";
type T0 = TypeName<string>; // "string"
type T1 = TypeName<"a">; // "string"
View lookup-types.ts
interface Person {
name: string;
age: number;
location: string;
}
type P1 = Person["name"]; // string
type P2 = string[][0]; // string
type P3 = string[]["length"]; // number
type P4 = [string]["length"]; // 1
View fibonacci.js
function fibonacci(num) {
if (num <= 1) {
return 1;
}
return fibonacci(num - 1) + fibonacci(num - 2);
}