Skip to content

Instantly share code, notes, and snippets.

@rajinwonderland
Created September 15, 2021 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajinwonderland/a1d0195e6c69799baf24b43a27743029 to your computer and use it in GitHub Desktop.
Save rajinwonderland/a1d0195e6c69799baf24b43a27743029 to your computer and use it in GitHub Desktop.
Determining an arguments type from an argument (typescript)
interface Author {
id: string;
firstName: string;
lastName: string;
address: Address;
email: string;
posts: Post[]
}
interface Post {
id: string;
author: Author;
content: string;
}
function instanceOfPerson(object: any): object is Persone {
return "firstName" in object
}
function instanceOfPost(object: any): object is Post {
return "content" in object
}
function determinePostOrPerson(obj: Post | Page){
if(instanceOfPerson(obj)){
return "Person"
}
if(instanceOfPage(obj)){
return "Page"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment