🔴

TypeError: XXX is not a constructor でちょっとハマった

2024/05/09に公開

次のようなエラーが出たのだ。

TypeError: CustomerServiceImpl is not a constructor
      9| export const customerService: CustomerService = new CustomerServiceI
       |                                                 ^

結論

モジュールの呼び出し先より下に定義された別のモジュールを、呼び出されるモジュールが使用していたのが原因だったのだ。

詳しく

CustomerServiceImpl のあるメソッドで、AddOrderException という例外クラスを使用していたのだ。このクラスが定義されているのが、new CustomerServiceImpl() より下にあったのだ。

export class CustomerServiceImpl implements CustomerService {
  // ...
    addOrder() {
        // ...
        const err = new AddOrderException('...');
        return Result.failure(err);
    }
}
export const customerService: CustomerService = new CustomerServiceImpl();

// ...

export class AddOrderException {
  // ...
}

AddOrderException を別ファイルに移動したら直ったのだ。

Discussion