Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active May 7, 2019 17:02
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 Shelob9/c87f480ad4acdccc16f209126f262464 to your computer and use it in GitHub Desktop.
Save Shelob9/c87f480ad4acdccc16f209126f262464 to your computer and use it in GitHub Desktop.
const string = 'Hi Roy';
console.log(string);//logs a string
console.log(string.slice(4));//logs result of the slice method of string
/**
* 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;
}
/**
* 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;
}
const PostTitle = (props: { postTitle: PostTitle}) => (
<h2>{props.postTitle.rendered}</h2>
);
/**
* 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>
);
};
/**
* 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>
);
};
/**
* 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>
);
};
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>
);
};
/**
* 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>
);
};
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;
}
}
<?php
interface PostContract {
/**
* Get post ID
*
* @return int
*/
public function getID() : string;
/**
*
* @param int $ID
*
* @return Post
*/
public function setID(int $ID): PostContract;
}
<?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;
}
}
<?php
function addFloatToIntegerCastToString(float $a,int $b) : string
{
return (string) $a * $b;
}
function addFloatToIntegerCastToString(a: number, b: number): string {
return (a + b).toString();
}
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