Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Last active June 15, 2021 08:00
Show Gist options
  • Select an option

  • Save mildfuzz/ffc6ce877bafd05f8cbd76b0db7c6bd2 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/ffc6ce877bafd05f8cbd76b0db7c6bd2 to your computer and use it in GitHub Desktop.
Useful Typescript
// 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