cossack910 / reactAd

レンダリング、CSS、ルーティング
0 stars 0 forks source link

型定義のpickとomit #9

Open cossack910 opened 11 months ago

cossack910 commented 11 months ago

interface

export interface CommentInterface {
  postId: number;
  id: number;
  name: string;
  email: string;
  body: string;
}
cossack910 commented 11 months ago

pick

必要なものだけ

import { CommentInterface } from "./interface/comment";

const dotComCheckFunc = (text: string) => {
  if (~text.indexOf(".com")) {
    return true;
  }
  return false;
};

export const Comment = (
  props: Pick<CommentInterface, "body" | "id" | "email">←これ
) => {
  const { body, id, email } = props;
  const emailDotComCheck: string = dotComCheckFunc(email)
    ? "[.com]"
    : "[ドットコム違う]";
  return <p>{`${emailDotComCheck}..${body}(コメントID:${id})`}</p>;
};

export default Comment;
cossack910 commented 11 months ago

Omit

不要なものを取り除く

import { CommentInterface } from "./interface/comment";

const dotComCheckFunc = (text: string) => {
  if (~text.indexOf(".com")) {
    return true;
  }
  return false;
};

export const Comment = (
  props:Omit<CommentInterface, "postId" | "name">←これ
) => {
  const { body, id, email } = props;
  const emailDotComCheck: string = dotComCheckFunc(email)
    ? "[.com]"
    : "[ドットコム違う]";
  return <p>{`${emailDotComCheck}..${body}(コメントID:${id})`}</p>;
};

export default Comment;