Tsoa: Interfaces declared by TypeScript can't be used

Created on 7 May 2020  路  14Comments  路  Source: lukeautry/tsoa

Sorting

  • I'm submitting a ...

    • [x] bug report
    • [ ] feature request
    • [ ] support request
  • I confirm that I

    • [x] used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

No error.

Current Behavior

TypeError: Cannot use 'in' operator to search for 'typeParameters' in undefined
    at TypeResolver.typeArgumentsToContext (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:776:47)
    at TypeResolver.getReferenceType (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:448:14)
    at TypeResolver.resolve (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:318:34)
    at TypeResolver.propertyFromSignature (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:721:119)
    at /mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:687:161
    at Array.map (<anonymous>)
    at TypeResolver.getModelProperties (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:687:124)
    at TypeResolver.getModelReference (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:512:31)
    at TypeResolver.getReferenceType (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:476:38)
    at TypeResolver.resolve (/mntnode_modules/tsoa/dist/metadataGeneration/typeResolver.js:318:34)

Steps to Reproduce

Create a method with:

@Get()
public returnNumber(): Number 

Context (Environment)

Version of the library: 3.0.8
Version of NodeJS: 10

  • Confirm you were using yarn not npm: [x] :laughing:

Detailed Description

Most helpful comment

I'm sure there's a bug here, what I'm saying is this:

interface Bug {
  facebook: {};
  twitter: {};
  google: {};
}

@Route("/")
@provideSingleton(MyController)
export class MyController extends Controller {
  @Get("/test")
  public test(): Bug {
    return {
      facebook: {},
      google: {},
      twitter: {},
    };
  }
}

doesn't cause issues for me.

The Number type does though.

All 14 comments

Having fooBar: Number; also triggers the error

fooBar: Number; is not a type, but the NumberConstructor, not sure if that was intended.
Regarding your interface, I don't think I can reproduce that given the information provided.

fooBar: Number; is not a type, but the NumberConstructor, not sure if that was intended.
Regarding your interface, I don't think I can reproduce that given the information provided.

Maybe try as a response interface content

    facebook: {};
    twitter: {};
    google: {};

instead

This looks like a bug :)

I'm sure there's a bug here, what I'm saying is this:

interface Bug {
  facebook: {};
  twitter: {};
  google: {};
}

@Route("/")
@provideSingleton(MyController)
export class MyController extends Controller {
  @Get("/test")
  public test(): Bug {
    return {
      facebook: {},
      google: {},
      twitter: {},
    };
  }
}

doesn't cause issues for me.

The Number type does though.

Okay, then just try @Response<Bug>('ok')

  @Get("/test")
  @Response<Bug>("ok")
  public test(): Bug {
    return {
      facebook: {},
      google: {},
      twitter: {},
    };
  }

doesn't cause any issues either, shows up in SwUI aswell :confused:

  @Get("/test")
  @Response<Bug>("ok")
  public test(): Bug {
    return {
      facebook: {},
      google: {},
      twitter: {},
    };
  }

doesn't cause any issues either, shows up in SwUI aswell

I tried once more and did not have the error.
The trace should be enough to fix the bug, let me know if you need more test cases

Yup, the Number is causing issues here (not number). I just tried making sure a very basic case like an interface with some nested props wouldn't cause issues in tsoa.

Just as a hint for other ppl. trying to use interfaces (not type aliases) from the ts lib declarations:
We explicitly filter them currently to avoid issues with duplicated interfaces.
Relates to https://github.com/lukeautry/tsoa/issues/333#issuecomment-625258178

@williamdes mind if I edit the description to better reflect the issue?

Go ahead ;)

I am getting this error with tsoa 3 for a interface we have to mimic JSON content:

export declare type JsonPrimitive = string | number | boolean | null;
export interface JsonObject {
    [member: string]: JsonValue;
}
export interface JsonArray extends Array<JsonValue> {}
export declare type JsonValue = JsonPrimitive | JsonObject | JsonArray;
// ...
export interface MyDto {
  data: JsonValue;
}
// ...
{  @Get("")
  public async someAction(): Promise<myDto> {
  }
}

This leads to the following error:

There was a problem resolving type of 'JsonArray'.
There was a problem resolving type of 'JsonValue'.
There was a problem resolving type of 'MyDto'.
Generate routes error.
 TypeError: Cannot use 'in' operator to search for 'typeParameters' in undefined
    at TypeResolver.typeArgumentsToContext (/home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:776:47)
    at /home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:814:38
    at Array.forEach (<anonymous>)
    at /home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:811:26
    at Array.forEach (<anonymous>)
    at TypeResolver.getModelInheritedProperties (/home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:807:25)
    at TypeResolver.getModelReference (/home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:514:40)
    at TypeResolver.getReferenceType (/home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:476:38)
    at TypeResolver.resolve (/home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:318:34)
    at /home/<user>/my-project/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:112:95

This did not occur with tsoa 2.5. Any ideas what I can change to make this work?

This didn't happen in 2.5 because it just didn't even try to generate docs for these type aliases.
I'm assuming this is happening bc. of the way you are declaring the interface here:
export interface JsonArray extends Array<JsonValue> {}
Can you try type JsonArray = Array<JsonValue> instead?

This is a different issue though. Maybe try and move this to a separate issue.

Thank you, this resolves the tsoa issue (although it creates a new typescript issue but that has nothing to do with tsoa).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Izabrr picture Izabrr  路  4Comments

jeremyVignelles picture jeremyVignelles  路  3Comments

ancizararenas picture ancizararenas  路  3Comments

jakubzloczewski picture jakubzloczewski  路  6Comments

cedricboidin picture cedricboidin  路  5Comments