Skip to content

Instantly share code, notes, and snippets.

@yowchun93
Last active April 6, 2020 02:39
Show Gist options
  • Save yowchun93/f122fe890e52a0d7f30e4ecbf646040e to your computer and use it in GitHub Desktop.
Save yowchun93/f122fe890e52a0d7f30e4ecbf646040e to your computer and use it in GitHub Desktop.
Practising TS generics
// create a function which takes an array of any types, and return a value of that type
// create a function which takes 2 values of any type and return an array of the type
// create a function called 'makeFullName', which takes an object
// create a function called 'makeFullName', which takes an object with attribute { firstName, lastName }
// making it optional
interface UserName {
firstName: string
lastName: string
fullName?: string
}
// not sure how to make this work
const makeFullName = (obj: UserName): (obj: UserName) => {
return {
...obj,
fullName: obj.firstName + " " + obj.lastName
}
}
const yc = makeFullName({firstName: 'yap', lastName: 'yowchun'})
console.log(yc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment