0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Parameter cannot have question mark and initializer. の解決方法

0
Posted at

概要

E2Eテストでログインを行うヘルパー関数を作成した際に、Parameter cannot have question mark and initializer. というエラーが発生したため、解決方法をまとめます。

やりたいこと

E2Eテストでログインを行うヘルパー関数を作成する。

実行したコード

export const login = async (
  page: Page,
  userName?: string = loginUser.userName,
  password?: string = loginUser.password,
) => {
  await page
    .getByRole("textbox", { name: loginTexts.userNameInput })
    .fill(userName);
  await page.getByLabel(loginTexts.passwordInput).fill(password);
  await page.getByRole("button", { name: loginTexts.loginButton }).click();
};

エラー内容

userName と password の引数で以下のエラーが発生しました。

Parameter cannot have question mark and initializer.

原因

TypeScriptでは、デフォルト値を指定した引数に ? は使用できません。

解決策

デフォルト値を指定した引数から ? を削除しました。

export const login = async (
  page: Page,
  userName: string = loginUser.userName,
  password: string = loginUser.password,
) => {
  await page
    .getByRole("textbox", { name: loginTexts.userNameInput })
    .fill(userName);
  await page.getByLabel(loginTexts.passwordInput).fill(password);
  await page.getByRole("button", { name: loginTexts.loginButton }).click();
};

学んだこと

TypeScriptでは、デフォルト値を指定した引数に ? を付けることはできません。

参考

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?