Created
September 15, 2021 19:21
-
-
Save rajinwonderland/a1d0195e6c69799baf24b43a27743029 to your computer and use it in GitHub Desktop.
Determining an arguments type from an argument (typescript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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