Skip to content

Commit 5d8ec8b

Browse files
committed
Fixed type errors on pages
1 parent 63a816f commit 5d8ec8b

15 files changed

+128
-128
lines changed

src/data/models/comment.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@ import * as HNDB from '../hn-data-api';
66
const logger = debug('app:Comment');
77

88
export class Comment {
9-
public readonly id;
10-
public readonly creationTime;
11-
public readonly comments;
12-
public readonly parent;
13-
public readonly submitterId;
14-
public readonly text;
9+
public readonly id: number;
10+
public readonly creationTime: number;
11+
public readonly comments: number[];
12+
public readonly parent: number;
13+
public readonly submitterId: string;
14+
public readonly upvotes: string[];
15+
public readonly text: string;
1516

16-
constructor(props) {
17-
if (!props.id) throw new Error(`Error instantiating Comment, id invalid: ${props.id}`);
18-
if (!props.parent) throw new Error(`Error instantiating Comment, parent invalid: ${props.parent}`);
19-
if (!props.submitterId) throw new Error(`Error instantiating Comment, submitterId invalid: ${props.submitterId}`);
20-
if (!props.text) throw new Error(`Error instantiating Comment, text invalid: ${props.text}`);
17+
constructor(fields) {
18+
if (!fields.id) throw new Error(`Error instantiating Comment, id invalid: ${fields.id}`);
19+
if (!fields.parent) throw new Error(`Error instantiating Comment, parent invalid: ${fields.parent}`);
20+
if (!fields.submitterId) throw new Error(`Error instantiating Comment, submitterId invalid: ${fields.submitterId}`);
21+
if (!fields.text) throw new Error(`Error instantiating Comment, text invalid: ${fields.text}`);
2122

22-
this.id = props.id;
23-
this.creationTime = props.creationTime || +new Date();
24-
this.comments = props.comments || [];
25-
this.parent = props.parent;
26-
this.submitterId = props.submitterId;
27-
this.text = props.text;
23+
this.id = fields.id;
24+
this.creationTime = fields.creationTime || +new Date();
25+
this.comments = fields.comments || [];
26+
this.parent = fields.parent;
27+
this.submitterId = fields.submitterId;
28+
this.text = fields.text;
29+
this.upvotes = fields.upvotes || [];
2830
}
2931
static getComment = id =>
3032
cache.getComment(id) || HNDB.fetchComment(id).catch(reason => logger(`Rejected comment: ${reason}`));

src/data/models/news-item.ts

+36-25
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,56 @@ const logger = debug('app:NewsItem');
1010
let newPostIdCounter = 100;
1111

1212
export class NewsItem {
13+
/** ID of submitter */
1314
public readonly id: number;
15+
/** Count of comments on the post */
1416
public readonly commentCount: number;
17+
/** List of comments */
1518
public readonly comments;
16-
public readonly creationTime;
17-
public readonly hides;
18-
public readonly submitterId;
19-
public readonly text: string;
19+
/** Post creation time, number of ms since 1970 */
20+
public readonly creationTime: number;
21+
/** IDs of users who hid the post */
22+
public readonly hides: string[];
23+
public readonly hiddenCount: number;
24+
/** ID of user who submitted */
25+
public readonly submitterId: string;
26+
/** Body text */
27+
public readonly text: string | null;
28+
/** Post title */
2029
public readonly title: string;
21-
public upvoteCount;
30+
/** Number of upvotes */
31+
public upvoteCount: number;
2232
public readonly upvotes;
2333
public readonly url;
2434

2535
public readonly hidden?: boolean; // TODO: exists?
2636

27-
constructor(props) {
28-
if (!props.id) {
29-
throw new Error(`Error instantiating News Item, id is required: ${props.id}`);
37+
constructor(fields) {
38+
if (!fields.id) {
39+
throw new Error(`Error instantiating News Item, id is required: ${fields.id}`);
3040
}
31-
if (!props.submitterId) {
32-
throw new Error(`Error instantiating News Item, submitterId is required: ${props.id}`);
41+
if (!fields.submitterId) {
42+
throw new Error(`Error instantiating News Item, submitterId is required: ${fields.id}`);
3343
}
34-
if (!props.title) {
35-
throw new Error(`Error instantiating News Item, title is required: ${props.id}`);
44+
if (!fields.title) {
45+
throw new Error(`Error instantiating News Item, title is required: ${fields.id}`);
3646
}
37-
if (props.url && !isValidUrl(props.url)) {
38-
throw new Error(`Error instantiating News Item ${props.id}, invalid URL: ${props.url}`);
47+
if (fields.url && !isValidUrl(fields.url)) {
48+
throw new Error(`Error instantiating News Item ${fields.id}, invalid URL: ${fields.url}`);
3949
}
4050

41-
this.id = props.id || (newPostIdCounter += 1);
42-
this.commentCount = props.commentCount || 0;
43-
this.comments = props.comments || [];
44-
this.creationTime = props.creationTime || +new Date();
45-
this.hides = props.hides || [];
46-
this.submitterId = props.submitterId;
47-
this.text = props.text || null;
48-
this.title = props.title;
49-
this.upvoteCount = props.upvoteCount || 1;
50-
this.upvotes = props.upvotes || [props.submitterId];
51-
this.url = props.url;
51+
this.id = fields.id || (newPostIdCounter += 1);
52+
this.commentCount = fields.commentCount || 0;
53+
this.comments = fields.comments || [];
54+
this.creationTime = fields.creationTime || +new Date();
55+
this.hides = fields.hides || [];
56+
this.hiddenCount = this.hides.length;
57+
this.submitterId = fields.submitterId;
58+
this.text = fields.text || null;
59+
this.title = fields.title;
60+
this.upvoteCount = fields.upvoteCount || 1;
61+
this.upvotes = fields.upvotes || [fields.submitterId];
62+
this.url = fields.url;
5263
}
5364

5465
static getNewsItem = id => cache.getNewsItem(id) || DB.getNewsItem(id) || HNDB.fetchNewsItem(id);

0 commit comments

Comments
 (0)