Skip to content

Instantly share code, notes, and snippets.

View fostyfost's full-sized avatar
🤪
All you need is Half-Life 3

Fosty Fost fostyfost

🤪
All you need is Half-Life 3
View GitHub Profile
@fostyfost
fostyfost / is-any.ts
Last active July 27, 2022 12:44
IsAny TS helper
/**
* Checks if type `T` is the `any` type.
* @see https://stackoverflow.com/a/49928360/3406963
* @see https://github.com/dsherret/conditional-type-checks/blob/main/mod.ts
*/
type IsAny<T> = 0 extends 1 & T ? true : false
type Example<S> = IsAny<S> extends false ? 'ok' : never
/**
@fostyfost
fostyfost / index.ts
Created July 28, 2022 13:26
Union to Type
type UnionToType<
U extends Record<string, unknown>
> = {
[K in (U extends unknown ? keyof U : never)]: U extends unknown
? K extends keyof U ? U[K] : never
: never
}
type A = {
a: string
@fostyfost
fostyfost / index.ts
Created July 28, 2022 13:29
Writable (remove `readonly`)
type Writeable<T> = {
-readonly [Key in keyof T]: T[Key]
}
type DeepWriteable<T> = {
-readonly [Key in keyof T]: DeepWriteable<T[Key]>
}
@fostyfost
fostyfost / index.ts
Created September 18, 2022 15:37
Extract Type
// @see https://stackoverflow.com/questions/60449479/typescript-extract-type-from-object-by-object-path-in-tuple
type ExtractType<O, T extends Array<any>> = {
[K in keyof O]: ((...a: T) => any) extends (a: any, ...args: infer Tail) => any
? Tail['length'] extends 0
? O[K]
: ExtractType<O[K], Tail>
: never
}[T[0]]
@fostyfost
fostyfost / index.ts
Created September 18, 2022 21:21
Non Empty Array
type NonEmptyArray<T> = [T, ...T[]]
@fostyfost
fostyfost / case1.ts
Created September 18, 2022 21:22
Union to Intersection
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
@fostyfost
fostyfost / index.ts
Created October 13, 2022 18:38
Required Field
export type RequiredField<T, K extends keyof T> = T & Required<Pick<T, K>>
@fostyfost
fostyfost / is-in-app.ts
Created October 21, 2022 09:52
Check `WebView` user agent for in-app cases
// @see https://stackoverflow.com/questions/71459572/not-possible-to-detect-webview-with-javascript-and-user-agent-on-old-android-ver
const rules = [
// If it says it's a webview, let's go with that.
'WebView',
// iOS webview will be the same as safari but missing "Safari".
'(iPhone|iPod|iPad)(?!.*Safari)',
// https://developer.chrome.com/docs/multidevice/user-agent/#webview_user_agent
'Android.*Version/[0-9].[0-9]',
// Also, we should save the wv detected for Lollipop.
// Android Lollipop and Above: webview will be the same as native,
@fostyfost
fostyfost / vosually-hidden.css
Created November 11, 2022 14:06
Visually hidden CSS
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
border: 0;
padding: 0;
white-space: nowrap;
clip-path: inset(100%);
clip: rect(0 0 0 0);
@fostyfost
fostyfost / get-circle-path.ts
Created November 30, 2022 14:08
SVG: Get circle path
export const getCirclePath = (cx: number | undefined = 0, cy: number | undefined = 0, r: number | undefined = 0): string => {
return `M ${cx - r}, ${cy} a ${r}, ${r} 0 1,0 ${r * 2},0 a ${r},${r} 0 1,0 ${r * -2},0`
}