4

I'm facing a problem. I need to expose a 'type' in an @ApiProperty of swagger on my API. But swagger don't accept it. I looked in many website to find solution but I did not find any.

Here is the error I get:

TS2693: 'testTest' only refers to a type, but is being used as a value here.

type testTest = 'A' | 'B';

    @ApiProperty({
  type: testTest,
  example: 'fr',
})
test: testTest;

I can't use something else since the type I need to use is from an external library.

3
  • 1
    You can't. Swagger needs a value, the types are all erased in compilation. Commented Apr 15, 2022 at 12:52
  • 1
    Sure, you totally right. But: how can I convert a type to an array of values of the keys of the type ? Commented Apr 15, 2022 at 12:57
  • you can't. Also that type would be an string. So type: String. And you will need to set up some sort of validation to ensure that test is either 'A' or 'B' Commented Apr 15, 2022 at 14:01

1 Answer 1

2

You cannot use a TypeScript type as a value for type in Swagger because type you are importing from the said library is not available at runtime, or once its transpiled to javascript. its only used for type checking. Thats how typescript works. This is same for any type, interfaces, return types etc...

What I suggest you do is to have a variable hold the values in type testTest = 'A' | 'B';

const testTest = ['A','B'];

@ApiProperty({
  type: String,
  example: testTest[0],
  enum: testTest
})
test: testTest;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.