Skip to content

Instantly share code, notes, and snippets.

View Luisgustavom1's full-sized avatar

Luis Gustavo Macedo Luisgustavom1

View GitHub Profile
type Diff<
Obj1,
Obj2,
SameKeys extends keyof Obj1 = keyof Obj1 & keyof Obj2
> = Omit<{
[K in (keyof Obj1 | keyof Obj2)]:
K extends keyof Obj1
? Obj1[K]
: K extends keyof Obj2
? Obj2[K]
@Luisgustavom1
Luisgustavom1 / Reverse.ts
Last active August 25, 2022 12:55
This is a reverse string using type level of typescript
type Reverse<A extends string> =
`${A}` extends `${infer H}${infer T}`
? `${Reverse<T>}${H}`
: A
@Luisgustavom1
Luisgustavom1 / kebabcase.ts
Created July 31, 2022 23:04
Type-challenges - 612 Medium kebabcase
// 00612 - Medium kebabcase
type KebabCase<T extends string> =
T extends `${infer F}${infer R}`
? R extends Uncapitalize<R>
? `${Uncapitalize<F>}${KebabCase<R>}`
: `${Uncapitalize<F>}-${KebabCase<R>}`
: T
export type KebabCaseResult = KebabCase<'FooBarBaz'> //foo-bar-baz
type PromiseAllType<P> =
P extends []
? []
: P extends [Promise<infer T> | infer T, ...infer F]
? [T, ...PromiseAllType<F>]
: never
declare function PromiseAll<P extends any[]>(args: readonly [...P]): Promise<PromiseAllType<P>>
const promise1 = Promise.resolve(3);
@Luisgustavom1
Luisgustavom1 / type-helpers.ts
Last active July 31, 2022 16:36
This is a types helper based on the specific types of properties. Created in this article to practice and study https://dev.to/busypeoples/notes-on-typescript-type-level-programming-part-3-3l03
type User = {
id: number,
name: string,
age: number,
active: boolean,
}
type FilterByType<T, U> = {
[K in keyof T]: T[K] extends U ? K : never
}[keyof T]