Typescript Enum array raises CircularDependencyError

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

using an enum array in a typescript object causes this error:

Error: A circular dependency has been detected (property key: "roles").
Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").

not the same, but maybe related to #507

Expected behavior

no error

Minimal reproduction of the problem with instructions

GitHub Repository to reproduce the issue.

  • clone the repo
  • npm install
  • npm run start:api

See file: create-cat.dto.ts

export class CreateCatDto {
  readonly name: string;
  readonly age: number;
  readonly breed: string;

  /**
   * using an enum array causes this exception:
   *
   *   Error: A circular dependency has been detected (property key: "roles").
   *   Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
   */
  readonly roles: RoleEnum[];
}

Environment


Nest version: 7.4.2
 
For Tooling issues:
- Node version: 14.3.0
- Platform:  Windows 10 64-bit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
tmtroncommented, Aug 25, 2020

Follow-up: I managed to workaround the issue using this comment:

  • here is a branch of my repo where the workaround is applied
  • Note that this workaround may be a bad idea
1reaction
nartccommented, Aug 24, 2020

@tmtron I looked at your repo and was able to run with the following changes:

export class Cat {
  @ApiProperty({
    example: 'Maine Coon',
    description: 'The breed of the Cat',
    required: true,
    isArray: true,
    enum: RoleEnum,
  })
  readonly roles?: RoleEnum[];
}

I don’t know where you get type: () => [RoleEnum] from for enum field. That syntax is specifically for nested classes. Can you make that same change in your code to see if it works?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert a typescript enum into an array of enums
1. I want to retain the type as enum. · 3. I believe you can use Object. · 6. Element implicitly has an...
Read more >
TypeScript enums vs. types for writing readable code
Enums allow us to define or declare a collection of related values that can be numbers or strings as a set of named...
Read more >
Convert a TypeScript Enum to a JavaScript Array or String
Converting a TypeScript Enum to a JavaScript Array is pretty easy, and I often use it to fill a <select> with all the...
Read more >
Enums in TypeScript - George Marklow - Medium
An enum is a set of labeled values called members and is used to organize a group of similar values. A typical data...
Read more >
Should You Use Enums or Union Types in Typescript?
While using TypeScript, there's something that we need to do quite often: define ... Can't iterate over a type, defining an array is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found