Skip to content

Commit c3c5ed3

Browse files
Add types for Mvc4 sample
1 parent 1944d9f commit c3c5ed3

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

src/React.Sample.Mvc4/Content/Sample.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,36 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
type Foo = {}
10+
type AuthorProps = {
11+
Name: string;
12+
GithubUsername: string;
13+
};
1114

12-
function CommentsBox(props) {
15+
type CommentProps = {
16+
Author: AuthorProps;
17+
Text: string;
18+
};
19+
20+
type CommentsBoxProps = {
21+
initialComments: CommentProps[];
22+
page: number;
23+
};
24+
25+
function CommentsBox(props: CommentsBoxProps) {
1326
let [state, updateState] = React.useState({
1427
comments: props.initialComments,
1528
page: props.page,
1629
hasMore: true,
17-
loadingMore: false
30+
loadingMore: false,
1831
});
1932

20-
function loadMoreClicked(evt) {
33+
function loadMoreClicked(evt: { preventDefault: () => void }) {
2134
let nextPage = state.page + 1;
2235
let comments = state.comments;
2336
updateState(prevState => ({
2437
...prevState,
2538
page: nextPage,
26-
loadingMore: true
39+
loadingMore: true,
2740
}));
2841

2942
let url = '/comments/page-' + (state.page + 1);
@@ -37,15 +50,15 @@ function CommentsBox(props) {
3750
...prevState,
3851
comments: comments.concat(data.comments),
3952
hasMore: data.hasMore,
40-
loadingMore: false
53+
loadingMore: false,
4154
}));
4255
};
4356
xhr.send();
4457
evt.preventDefault();
4558
}
4659

47-
let commentNodes = state.comments.map(comment => (
48-
<Comment author={comment.Author}>{comment.Text}</Comment>
60+
let commentNodes = state.comments.map((comment: CommentProps) => (
61+
<CommentRow author={comment.Author}>{comment.Text}</CommentRow>
4962
));
5063

5164
function renderMoreLink() {
@@ -72,9 +85,9 @@ function CommentsBox(props) {
7285
);
7386
}
7487

75-
class Comment extends React.Component {
88+
class CommentRow extends React.Component<{ author: AuthorProps }> {
7689
static propTypes = {
77-
author: PropTypes.object.isRequired
90+
author: PropTypes.object.isRequired,
7891
};
7992

8093
render() {
@@ -89,9 +102,9 @@ class Comment extends React.Component {
89102
}
90103
}
91104

92-
class Avatar extends React.Component {
105+
class Avatar extends React.Component<{ author: AuthorProps }> {
93106
static propTypes = {
94-
author: PropTypes.object.isRequired
107+
author: PropTypes.object.isRequired,
95108
};
96109

97110
render() {

src/React.Sample.Mvc4/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "React.Sample.Mvc4",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"devDependencies": {
7+
"@types/react": "^16.8.12",
8+
"@types/react-dom": "^16.8.3",
9+
"@types/reactstrap": "^7.1.3",
10+
"typescript": "^3.4.3"
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": false,
4+
"module": "commonjs",
5+
"target": "es6",
6+
"jsx": "react",
7+
"esModuleInterop": true,
8+
"noEmit": true,
9+
"types": ["./types"],
10+
"lib": ["es2015", "dom"]
11+
},
12+
"include": ["./Content/**/*"]
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
Component as _Component,
3+
useState as _useState,
4+
Dispatch,
5+
SetStateAction,
6+
} from 'react';
7+
8+
// Globally available modules must be declared here
9+
// Copy type definitions from @types/react/index.d.ts, because namespaces can't be re-exported
10+
11+
declare global {
12+
namespace React {
13+
function useState<S>(
14+
initialState: S | (() => S),
15+
): [S, Dispatch<SetStateAction<S>>];
16+
function useState<S = undefined>(): [
17+
S | undefined,
18+
Dispatch<SetStateAction<S | undefined>>
19+
];
20+
interface Component<P = {}, S = {}, SS = any>
21+
extends ComponentLifecycle<P, S, SS> {}
22+
}
23+
const Reactstrap: any;
24+
const PropTypes: any;
25+
}
26+
27+
export const test = 1;

0 commit comments

Comments
 (0)