LoginSignup
0
1

プロパティのショートハンド記法

Posted at

初めに

buildの実行で発生したエラーを解決できたので、共有します。

問題

res.status(500).json({ error.error }); で以下エラーが発生しました。

Expected property shorthand.eslint(object-shorthand)

import { NextApiRequest, NextApiResponse } from 'next';
import { todoData } from '@/data/todoData';

const handler =  (req: NextApiRequest, res: NextApiResponse) => {
  try {
    res.status(200).json(todoData);
  } catch (error) {
    res.status(500).json({ error.error });
  }
};

export default handler;

原因

ESLintのルールであるobject-shorthandがプロパティのショートハンド記法の使用を期待しているからです。このルールは、オブジェクトリテラル内で変数名とプロパティ名が同じ場合に、ショートハンド記法を使うことを推奨しています。

解決方法

エラーも消えて、記述量も減りました。

res.status(500).json({ error });

終わりに

ESLintのルールを知れたので、学びになりました。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1