-
-
Save Shelob9/c87f480ad4acdccc16f209126f262464 to your computer and use it in GitHub Desktop.
This file contains 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
const string = 'Hi Roy'; | |
console.log(string);//logs a string | |
console.log(string.slice(4));//logs result of the slice method of string | |
This file contains 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 descrinbing a WordPress post | |
*/ | |
interface Post { | |
title: { | |
//This property is always present | |
rendered: string; | |
//This property is only present in some contexts | |
raw?: string; | |
}, | |
id: number; | |
} |
This file contains 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 for post title, content and excerpt | |
*/ | |
interface ContentObject { | |
//This property is always present | |
rendered: string; | |
//This property is only present in some contexts | |
raw?: string; | |
} | |
/** | |
* Interface for describing post title | |
*/ | |
interface PostTitle extends ContentObject {} | |
/** | |
* Interface for describing post content | |
*/ | |
interface PostContent extends ContentObject {} | |
/** | |
* Interface for describing post content | |
*/ | |
interface PostExcerpt extends ContentObject {} | |
/** | |
* Interface descrinbing a WordPress post | |
*/ | |
interface Post { | |
title: PostTitle; | |
content: PostContent; | |
excerpt: PostExcerpt; | |
date: string; | |
id: number; | |
} |
This file contains 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
const PostTitle = (props: { postTitle: PostTitle}) => ( | |
<h2>{props.postTitle.rendered}</h2> | |
); |
This file contains 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
/** | |
* Display A Blog Post | |
* | |
* @param props | |
*/ | |
const BlogPost = (props: { post: Post; showContent: boolean }) => { | |
const { post, showContent } = props; | |
function createMarkup(markup: string) { | |
return { __html: markup }; | |
} | |
return ( | |
<article id={`post-${post.id}`}> | |
<h1>{post.title.rendered}</h1> | |
<p>Published {moment(post.date).fromNow()}</p> | |
<div> | |
{showContent ? ( | |
<div dangerouslySetInnerHTML={createMarkup(post.content.rendered)} /> | |
) : ( | |
<div dangerouslySetInnerHTML={createMarkup(post.excerpt.rendered)} /> | |
)} | |
</div> | |
</article> | |
); | |
}; |
This file contains 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
/** | |
* Display a list of posts | |
* | |
* @param props | |
*/ | |
const ListOfPosts = (props: { | |
posts: Array<Post>; | |
showContent: boolean; | |
toggleShowContent: (enable: boolean) => void; | |
}) => { | |
const { posts, showContent, toggleShowContent } = props; | |
return ( | |
<div> | |
{/** **/} | |
</div> | |
); | |
}; |
This file contains 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
/** | |
* Display a list of posts | |
* | |
* @param props | |
*/ | |
const ListOfPosts = (props: { | |
posts: Array<Post>; | |
showContent: boolean; | |
toggleShowContent: (enable: boolean) => void; | |
}) => { | |
const { posts, showContent, toggleShowContent } = props; | |
return ( | |
<div> | |
<button /> | |
<Fragment> | |
{posts.map((post: Post) => ( | |
<BlogPost key={post.id} post={post} showContent={showContent} /> | |
))} | |
</Fragment> | |
</div> | |
); | |
}; |
This file contains 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
const ListOfPosts = (props: { | |
posts: Array<Post>; | |
showContent: boolean; | |
toggleShowContent: (enable: boolean) => void; | |
}) => { | |
const { posts, showContent, toggleShowContent } = props; | |
return ( | |
<div> | |
<button | |
onClick={(e:React.FormEvent<HTMLInputElement>) => { | |
e.preventDefault(); | |
toggleShowContent(!showContent); | |
}} | |
> | |
Show Full Content | |
</button> | |
<Fragment> | |
{posts.map((post: Post) => ( | |
<BlogPost key={post.id} post={post} showContent={showContent} /> | |
))} | |
</Fragment> | |
</div> | |
); | |
}; |
This file contains 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
/** | |
* Control for remote API URL | |
* @param props | |
*/ | |
const ApiUrl = (props: { | |
url: string; | |
changeHandler: (newValue: string) => void; | |
}) => { | |
const { url, changeHandler } = props; | |
return ( | |
<div> | |
<label htmlFor={'remote-api'}>Remote API URL</label> | |
<input | |
id={'remote-api'} | |
type={'url'} | |
value={url} | |
onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | |
e.preventDefault(); | |
changeHandler(e.target.value); | |
}} | |
/> | |
</div> | |
); | |
}; |
This file contains 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
class Post { | |
/** | |
* Post ID | |
* | |
* @var int | |
*/ | |
protected $ID; | |
/** | |
* Get post ID | |
* | |
* @return int | |
*/ | |
public function getID() : string | |
{ | |
return $this->ID; | |
} | |
/** | |
* | |
* @param int $ID | |
* | |
* @return Post | |
*/ | |
public function setID(int $ID): Post | |
{ | |
$this->ID = $ID; | |
return $this; | |
} | |
} |
This file contains 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
<?php | |
interface PostContract { | |
/** | |
* Get post ID | |
* | |
* @return int | |
*/ | |
public function getID() : string; | |
/** | |
* | |
* @param int $ID | |
* | |
* @return Post | |
*/ | |
public function setID(int $ID): PostContract; | |
} |
This file contains 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
<?php | |
class Post implements PostContract{ | |
/** | |
* Post ID | |
* | |
* @var int | |
*/ | |
protected $ID; | |
/** | |
* Get post ID | |
* | |
* @return int | |
*/ | |
public function getID() : string | |
{ | |
return $this->ID; | |
} | |
/** | |
* | |
* @param int $ID | |
* | |
* @return Post | |
*/ | |
public function setID(int $ID): PostContract | |
{ | |
$this->ID = $ID; | |
return $this; | |
} | |
} |
This file contains 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
<?php | |
function addFloatToIntegerCastToString(float $a,int $b) : string | |
{ | |
return (string) $a * $b; | |
} |
This file contains 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
function addFloatToIntegerCastToString(a: number, b: number): string { | |
return (a + b).toString(); | |
} |
This file contains 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
npx create-react-app learn-type-script --typescript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment