Skip to content

Instantly share code, notes, and snippets.

View olafsulich's full-sized avatar
🎯
Focusing

Olaf Sulich olafsulich

🎯
Focusing
View GitHub Profile
const fruits = ['Apple 🍏', 'Banana 🍌', 'Strawberry 🍓', 'Blueberry 🫐'];
// Wybieranie po indexie
✅ fruits.at(0); // Apple 🍏
✅ fruits.at(3); // Blueberry 🫐
.box {
background-image: url("large-balloons.jpg");
background-image: image-set(
url("large-balloons.avif") type("image/avif"),
url("large-balloons.jpg") type("image/jpeg"));
}
@olafsulich
olafsulich / assert.ts
Last active September 23, 2021 12:00
function assert<T>(
condition: T,
message,
): asserts condition is Exclude<T, null | undefined> {
if (condition === null || condition === undefined) {
throw new Error(message);
}
}
@olafsulich
olafsulich / recursive.ts
Created September 23, 2021 09:15
Recursive<T>
type Recurse<T> =
T extends { __rec: unknown }
? Recurse<_Recurse<T>>
: T;
type _Recurse<T> =
T extends { __rec: never } ? never
: T extends { __rec: { __rec: infer U } } ? { __rec: _Recurse<U> }
: T extends { __rec: infer U } ? U
: T;
@olafsulich
olafsulich / camelize.ts
Last active September 23, 2021 09:07
Turn your snake_case into camelCase (works for strings and objects)
// Usage: type CamelCase = CamelizeString<'snake_case'> -> snakeCase
export type CamelizeString<Value> =
Value extends string ?
Value extends `${infer T}_${infer U}` ?
`${T}${Capitalize<CamelizeString<U>>}` :
Value : Value
type KeyOf<Object extends Record<string, unknown>> = Extract<keyof Object, string>;
@olafsulich
olafsulich / parse-route-parameters.ts
Created September 7, 2021 20:35
TypeScript - parse route parameters
type ParseRouteParameters<Route> = Route extends `${string}/:${infer Param}/${infer Rest}`
? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string }
: Route extends `${string}/:${infer Param}`
? { [Entry in Param]: string }
: {};
type X = ParseRouteParameters<'/api/:what/:is/notyou/:happening'>;
// type X = {
// what: string,
@olafsulich
olafsulich / tuple-to-union-type.ts
Last active July 19, 2024 16:11
TypeScript - tuple to union type
type UnionFromTuple<Tuple extends readonly (string | number | boolean)[]> = Tuple[number];
const animals = ['🦙', '🦑', '🐍', '🦘'] as const;
type Animal = UnionFromTuple<typeof animals>;
// type Animal = '🦙' | '🦑' | '🐍' | '🦘'