Skip to content

Instantly share code, notes, and snippets.

@windbridges
Created June 3, 2021 17:53
Show Gist options
  • Save windbridges/4ec21d6ded069a79d4b6cfb4ddde7ec4 to your computer and use it in GitHub Desktop.
Save windbridges/4ec21d6ded069a79d4b6cfb4ddde7ec4 to your computer and use it in GitHub Desktop.
// Argument type based on value of another argument
type OrderType = 'Simple' | 'Extended'
interface OrderOptionsSet = {
Simple: {
name: string
age: number
},
Extended: {
firstName: string
lastName: string
something: any
}
}
async createTask<K extends OrderType>(type: K, options: OrderOptionsSet[K]): Promise<any> {
// options type now will be OrderOptionsSet.Simple or OrderOptionsSet.Extended here bdepending on type value
}
// Return type based on return type of function, passed as argument
export async function waitUntil<T>(fn: () => T, timeoutSec: number = 30): Promise<T> {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment