Last active
June 17, 2021 17:14
-
-
Save busypeoples/b2d9ecda58ff25f3206e8c171f04b087 to your computer and use it in GitHub Desktop.
Helper Functions in TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TS Helper Functions | |
*/ | |
type ConvertTo<Type, From, To> = { | |
[Key in keyof Type]: Required<Type>[Key] extends From ? To : Type[Key]; | |
}; | |
type FilterByType<T, U> = { | |
[Key in keyof Required<T>]: Required<T>[Key] extends U ? Key : never; | |
}[keyof T]; | |
type ExcludeByType<T, U> = Omit<T, FilterByType<T, U>>; | |
type IncludeByType<T, U> = Pick<T, FilterByType<T, U>>; | |
type ReadOnlyByType<T, U> = T & Readonly<IncludeByType<T, U>>; | |
type OptionalByType<T, U> = Partial<IncludeByType<T, U>> & ExcludeByType<T, U>; | |
type RequiredByType<T, U> = Partial<ExcludeByType<T, U>> & IncludeByType<T, U>; | |
type OnlyMakeTypeRequired<T, U> = ExcludeByType<T, U> & Required<IncludeByType<T, U>>; | |
// Test | |
type User = { | |
points: number; | |
name: string; | |
active: boolean; | |
}; | |
/** | |
type UserOne = { | |
points: number[]; | |
name: string; | |
active: boolean; | |
} | |
*/ | |
type UserOne = ConvertTo<User, number, number[]>; | |
/** | |
type UserTwo = { | |
name: string; | |
active: boolean; | |
} | |
*/ | |
type UserTwo = ExcludeByType<User, number>; | |
/** | |
type UserThree = { | |
points: number; | |
} | |
*/ | |
type UserThree = IncludeByType<User, number>; | |
/** | |
type UserFour = { | |
readonly points: number; | |
name: string; | |
active: boolean; | |
}; | |
*/ | |
type UserFour = ReadOnlyByType<User, number>; | |
/** | |
type UserFive = { | |
points?: number; | |
name: string; | |
active: boolean; | |
}; | |
*/ | |
type UserFive = OptionalByType<User, number>; | |
/** | |
type UserSix = { | |
points: number; | |
name?: string; | |
active?: boolean; | |
}; | |
*/ | |
type UserSix = RequiredByType<User, number>; | |
/** | |
type User = { | |
id: number; | |
name: string; | |
points: number; | |
active?: boolean; | |
} | |
*/ | |
type RequiredByTypeOnlyUser = OnlyMakeTypeRequired<{ | |
id: number; | |
points?: number; | |
name: string; | |
active?: boolean; | |
}, number>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment