Skip to content

Instantly share code, notes, and snippets.

View roalcantara's full-sized avatar
🤖
Don't Panic!

Rogério Rodrigues de Alcântara roalcantara

🤖
Don't Panic!
View GitHub Profile
@roalcantara
roalcantara / fix_xcode.sh
Last active February 11, 2021 17:22
OSX: xcode fixes
# xcode-select --install
# https://dev.to/kurab/check-list-before-re-install-xcode-command-line-tool-295l
$ sudo rm -rf /Library/Developer/CommandLineTools
$ sudo xcode-select --install
# OSX: Error: "xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist"
# https://github.com/Homebrew/brew/issues/1012
$ sudo xcode-select --switch /Library/Developer/CommandLineTools
@roalcantara
roalcantara / Drop.ts
Last active September 6, 2020 09:06
TypeScript 3.9 | Advanced Types: Takes a tuple T and drops the first N entries
type Tail<T extends any[]> = ((...t: T) => any) extends (
_: any,
...tail: infer TT
) => any
? TT
: []
type Prepend<E, T extends any[]> = ((head: E, ...args: T) => any) extends (
...args: infer U
) => any
@roalcantara
roalcantara / Prepend.ts
Last active September 6, 2020 09:06
TypeScript 3.9 | Advanced Types: Adds a type E at the top of a tuple T
type Prepend<E, T extends any[]> = ((head: E, ...args: T) => any) extends (
...args: infer U
) => any
? U
: T
type Prepend01 = Prepend<string, []> // [string]
type Prepend02 = Prepend<string, [1, 2]> // [string, 1, 2]
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
@roalcantara
roalcantara / Length.ts
Last active September 6, 2020 09:07
TypeScript 3.9 | Advanced Types: Returns an array's length
type Length<T extends any[]> = T['length']
type Length01 = Length<[]> // 0
type Length02 = Length<[any, any]> // 2
type Length03 = Length<[any, any, any]> // 3
type Length04 = Length<['a', 1, null, string]> // 4
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
// https://github.com/millsp/medium/blob/master/types-curry-ramda/src/index.ts
@roalcantara
roalcantara / Last.ts
Last active September 6, 2020 09:07
TypeScript 3.9 | Advanced Types: Extracts a tuple's last entry
type Head<T extends any[]> = T extends [any, ...any[]] ? T[0] : never
type HasTail<T extends any[]> = T extends [] | [any] ? false : true
type Tail<T extends any[]> = ((...t: T) => any) extends (
_: any,
...tail: infer TT
) => any
? TT
: []
type Last<T extends any[]> = {
@roalcantara
roalcantara / TupleInfer.ts
Last active September 6, 2020 09:07
TypeScript 3.9 | Advanced Types: Extracts types from a tuple
type TupleInfer<T> =
T extends [infer A, ...(infer B)[]]
? [A, B]
: never
type Type1 = TupleInfer<[name: string, age: number, single: boolean]> // [string, number | boolean]
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
// https://github.com/millsp/medium/blob/master/types-curry-ramda/src/index.ts
@roalcantara
roalcantara / ArrayInfer.ts
Last active September 6, 2020 09:08
TypeScript 3.9 | Advanced Types: Extracts types from an array
type ArrayInfer<T> =
T extends (infer U)[]
? U
: never
const array = [0, 'data', 1, 'foo']
type Type1 = ArrayInfer<typeof array> // string | number
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
@roalcantara
roalcantara / ClassInfer.ts
Last active September 6, 2020 09:08
TypeScript 3.9 | Advanced Types: Extracts generic types from a class or an interface
type ClassInfer<I> =
I extends Promise<infer G>
? G
: never
const promise = new Promise<string>(() => {})
type Type1 = ClassInfer<typeof promise> // string
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
@roalcantara
roalcantara / FunctionInfer.ts
Last active September 6, 2020 09:08
TypeScript 3.9 | Advanced Types: Extracts inner types and return type from function
export type FunctionInfer<F> =
F extends (...args: infer A) => infer R
? [A, R]
: never
const fn01 = (arg: number, foo: any) => arg === foo
export type Type1 = FunctionInfer<typeof fn01> // [[arg: number, foo: any], boolean]
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
@roalcantara
roalcantara / ObjectInfer.ts
Last active September 6, 2020 09:08
TypeScript 3.9 | Advanced Types: Extracts a property’s type from an object
type ObjectInfer<O> =
O extends { a: infer A }
? A
: never
const object = { a: 'Naruto' }
type Type1 = ObjectInfer<typeof object> // string
type Type2 = ObjectInfer<string> // never