Bläddra i källkod

create type guard

Shun Miyazawa 3 år sedan
förälder
incheckning
8538aebae2

+ 3 - 2
packages/app/src/components/InAppNotification/InAppNotificationList.tsx

@@ -2,7 +2,8 @@ import React, { FC } from 'react';
 
 import { HasObjectId } from '@growi/core';
 
-import { IInAppNotification, PaginateResult } from '~/interfaces/in-app-notification';
+import type { IInAppNotification, PaginateResult } from '~/interfaces/in-app-notification';
+import { isIPageSnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
 
 import InAppNotificationElm from './InAppNotificationElm';
 
@@ -26,7 +27,7 @@ const InAppNotificationList: FC<Props> = (props: Props) => {
     );
   }
 
-  const notifications = inAppNotificationData.docs;
+  const notifications = inAppNotificationData.docs.filter((notification) => { return isIPageSnapshot(notification.snapshot) });
 
   return (
     <>

+ 8 - 2
packages/app/src/models/serializers/in-app-notification-snapshot/page.ts

@@ -1,11 +1,17 @@
-import { IUser } from '~/interfaces/user';
-import { IPage } from '~/interfaces/page';
+import type { IPage } from '~/interfaces/page';
+import type { IUser } from '~/interfaces/user';
 
 export interface IPageSnapshot {
   path: string
   creator: IUser
 }
 
+// type guard
+// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+export const isIPageSnapshot = (args: any): args is IPageSnapshot => {
+  return args.path != null && args.creator != null;
+};
+
 export const stringifySnapshot = (page: IPage): string => {
   return JSON.stringify({
     path: page.path,