Created
October 28, 2020 15:09
-
-
Save ffflorian/54a775067134fab8f069221fa4f46be4 to your computer and use it in GitHub Desktop.
TypeScript Helpers
This file contains hidden or 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
// -- Unwrapping values from an array inside an interface -- | |
type Person = { | |
name: string; | |
birthDate?: string; | |
age: number; | |
hobbies: Array<{ title: string; location: string }> | |
} | |
type DeArrayify<A> = A extends Array<infer T> ? T : A; | |
type Hobby = DeArrayify<Person['hobbies']>; | |
// Hobby = { title: string, location: string } | |
// -- Unwrapping template values from interfaces with knockout Observable values -- | |
type Person2 = { | |
name: string; | |
birthDate?: string; | |
age: ko.Observable<number>; | |
} | |
type UnwrapObservable<I> = { | |
[K in keyof I]: I[K] extends ko.Observable ? I[K] extends ko.Observable<infer U> ? U : never : I[K]; | |
}; | |
type Unwrapped = UnwrapObservable<Person2>; | |
// Unwrapped = { name: string, birthDate?: string; age: number; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment