Last active
June 15, 2021 08:00
-
-
Save mildfuzz/ffc6ce877bafd05f8cbd76b0db7c6bd2 to your computer and use it in GitHub Desktop.
Useful Typescript
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
| // returns an optional type of properties of T where type satisfies U | |
| export type KeysOfValueType<T, U> = { | |
| [K in keyof T]: T[K] extends U ? K : never | |
| }[keyof T] | |
| // returns an optional type of properties of T where T[prop]=string | |
| export type StringsOf<T> = KeysOfValueType<T, string> | |
| // example of getting only string values from an object in a generic | |
| // https://github.com/microsoft/TypeScript/issues/44532 | |
| function innerLowerCase< | |
| T, K extends keyof T, | |
| >(obj: T & Record<string, string | number | Record<string, unknown>> & Record<K, string>, key: K) { | |
| const objString = obj[key] | |
| return objString.toLowerCase() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment