Skip to content

Instantly share code, notes, and snippets.

@mogarick
Last active July 18, 2021 03:31
Show Gist options
  • Save mogarick/ad040a42dc614ef5de43d56a2d922e32 to your computer and use it in GitHub Desktop.
Save mogarick/ad040a42dc614ef5de43d56a2d922e32 to your computer and use it in GitHub Desktop.
typescript complex example from twitter question
//From: https://www.typescriptlang.org/play?#code/C4TwDgpgBAEhA28D2AeACgJyZDwCWEAzlBAB7AQB2AJsRhAIbVKXwhSHAZ6UDmA2gF0AfFAC8UAN4AoKFDBYc+IgC4ombBFwFC0gL5QAZFABKEAMZIM1dIq3LC-SgFcAtgCMtggDRQXHrWFpaQB6EKhgAAtofgA6eI0lHUEoHgAzLWI8YCgGYgYI5zB4CGk050pzfBYoc3oGCjhEVET7HRJyKlooeuZWdk5uPiFhAApo5rUm5BQ4hLttIhEASilZHohgZwxKKAnkfWC6xkaEZFGZOQVNRcI1fgBySgZXCAffB4ZeN59159e1ABGABMAGZvOsvhA1AAWACsADZ9MtpEA
type Hello<Properties extends readonly string[]> = {
properties: Properties
} & Record<Properties[number], number>
// the [...Properties] infers it as a tuple
function createHello<Properties extends readonly string[]>(hello: Hello<[...Properties]>) {
return hello
}
createHello({
properties: ['name', 'age'],
name: 123,
age: 456
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment