Skip to content

Instantly share code, notes, and snippets.

View graffhyrum's full-sized avatar

Joshua Pendragon graffhyrum

View GitHub Profile

The TypeScript code snippet provided is a utility for working with objects in a type-safe manner. It defines a type Entries and a function getEntries.

The Entries type is a mapped type that transforms the properties of an object type T into an array of tuples. Each tuple contains the property key as its first element and the corresponding property value as its second element.

export type Entries<T> = {
  [K in keyof T]-?: [K, T[K]];
}[keyof T][];

The provided TypeScript code defines a utility type OneOfType that ensures an object can only have one property from the provided type. This is achieved using TypeScript's conditional and mapped types.

The OneOfType type is defined as follows:

export type OneOfType<T> = ValueOf<OneOfByKey<T>>;

Here, OneOfType is a mapped type that transforms an object type T into a type that allows only one property from T to be present at a time. It uses two helper types OneOfByKey and ValueOf.

@graffhyrum
graffhyrum / recursive_partial.md
Last active June 12, 2026 20:08
The provided TypeScript code defines a utility type RecursivePartial. This type is used to create a version of an existing type, but with all of its properties (and their nested properties) made optional and able to be partially provided.

The provided TypeScript code defines a utility type RecursivePartial. This type is used to create a version of an existing type, but with all of its properties (and their nested properties) made optional and able to be partially provided.

The RecursivePartial type is defined as a mapped type that iterates over all properties (Prop) of a given type (Type):

export type RecursivePartial<Type> = {
  [Prop in keyof Type]?: ...
};
@graffhyrum
graffhyrum / Prefix.ts
Last active January 1, 2024 00:56
Apply or Remove a prefix from a Record type
/**
* This utility type `Prefixed` is used to prefix the keys of a given object type.
* It takes two type parameters:
* - `Prefix` which is a string that will be used as the prefix.
* - `Base` which is the object type whose keys will be prefixed.
*
* The resulting type will have the same values as the `Base` type, but the keys will be prefixed with the `Prefix`.
*
* @template Prefix - The prefix to be added to the keys of the `Base` type.
* @template Base - The base object type whose keys will be prefixed.
@graffhyrum
graffhyrum / api_fetch.ts
Last active January 15, 2024 20:08
Type Safe fetch function
/**
* Type definition for a function that creates an API method.
* This function takes an options object with a url and a method type.
* It returns a function that takes an input object and returns a Promise of an output object.
*
* @template TInput - An object type that extends Record<string, string>.
* @template TOutput - The type of the output object.
*
* @param {Object} options - The options for the API method.
* @param {string} options.url - The URL for the API method.
@graffhyrum
graffhyrum / retryUntilSuccess.md
Last active February 15, 2024 21:59
Safe retry logic pattern

retryUntilSuccess Function

The retryUntilSuccess function is a utility function that allows you to retry a given function until it succeeds or until a maximum number of attempts has been reached.

Signature

function retryUntilSuccess<Fn extends RetryFunction>(f: Fn, n: number, ...args: Parameters<Fn>): boolean

Summary

The sendTabUntilFocused function is an asynchronous function in TypeScript that simulates the pressing of the 'Tab' key until a specific element on the webpage is focused. This function is useful in automated testing scenarios where you need to simulate user interactions with a webpage.

Signature

async function sendTabUntilFocused(page: Page, selector: ReturnType<typeof page.locator>): Promise<void>

Parameters

  • page: An instance of the Page class from the Playwright library, representing a single tab in a browser.
@graffhyrum
graffhyrum / PlaywrightShowcase.md
Last active June 4, 2024 23:09
Micro Playwright config example

These files are an illustration of the benefits available in playwright test projects. It leverages parallelization and composed fixtures to create a matrix of tests for different combinations of test environments, browsers, and resolutions. The following is a brief description of the files in their execution order:

  1. playwright.config.ts: This is the entry point for the Playwright tests. It defines the configuration for the tests and uses the getProjects function to generate a matrix of tests for different combinations of environments, browsers, and resolutions.

  2. getProjects.ts: This file defines a function getProjects that generates a matrix of tests for different combinations of environments, browsers, and resolutions. The function can be customized with specific environments, browsers, and resolutions to test.

  3. params.ts: This file defines several constants and types related to the testable environments, browsers, and resolutions. It also defines the types TestableEnvironment, `Testabl

@graffhyrum
graffhyrum / playwright.yaml
Created July 10, 2024 21:55
A good github action template for running playwright tests using Bun
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
@graffhyrum
graffhyrum / browserWrapper.ts
Last active July 26, 2024 20:18
Wrap some callback action in a new browser instance with setup and teardown handled
import {
BrowserContextOptions,
BrowserType,
LaunchOptions,
Page,
} from '@playwright/test';
function doWithNewBrowser(
callback: (page: Page, ...args: any[]) => Promise<any>,
browser: BrowserType,