tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

Exception "Can't support 'refAlias' type." is thrown when using Exclude<Type, ExcludedUnion> with Typescript >= 4.2.2

Open ggrossetie opened this issue 2 years ago • 6 comments

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

The following definition:

export type Severity = 'critical' | 'high' | 'medium' | 'low';

export type SecuritySeverity = Exclude<Severity, 'low'>;

should produce the following Tsoa.ReferenceType with Typescript >= 4.2.2:

{
  "dataType": "refAlias",
  "refName": "SecuritySeverity",
  "type": {
    "dataType": "refAlias",
    "description": "Exclude from T those types that are assignable to U",
    "refName": "Exclude_Severity.low_",
    "type": {
      "dataType": "union",
      "types": [
        {
          "dataType": "enum",
          "enums": [
            "critical"
          ]
        },
        {
          "dataType": "enum",
          "enums": [
            "high"
          ]
        },
        {
          "dataType": "enum",
          "enums": [
            "medium"
          ]
        }
      ]
    },
    "validators": {}
  },
  "validators": {}
}

Current Behavior

With Typescript 4.2.2, I get the following Tsoa.ReferenceType:

{
  "dataType": "refAlias",
  "description": "Exclude from T those types that are assignable to U",
  "refName": "Exclude_Severity.low_",
  "type": {
    "dataType": "refObject",
    "refName": "SecuritySeverity"
  },
  "validators": {}
}

Since I'm using this type in a Query parameter:

@Route('/test')
export class TestController {
  @Get('/issues')
  public async getSecurityIssues(@Query('type') securitySeverity: SecuritySeverity): Promise<void> {
    return Promise.resolve();
  }
}

I get the following exception:

Generate swagger error.
 Error: @Query('type') Can't support 'refAlias' type. 
 in 'TestController.getSecurityIssues'

The reason is that refObject is not a supported data type.

Possible Solution

I think the root cause is that the latest versions of Typescript now return an aliasSymbol:

https://github.com/lukeautry/tsoa/blob/ea976ee5cac9edba0a2fbc9eb8d2729c4237f491/packages/cli/src/metadataGeneration/typeResolver.ts#L206

With Typescript 4.1.2, the aliasSymbol is undefined and as a result we don't step into this if:

https://github.com/lukeautry/tsoa/blob/ea976ee5cac9edba0a2fbc9eb8d2729c4237f491/packages/cli/src/metadataGeneration/typeResolver.ts#L208-L226

Having said that, I'm not familiar enough with Typescript internals nor with this code base to suggest a fix, sorry 😞

Steps to Reproduce

Here's a sample project that reproduces this issue: https://github.com/Mogztter/bug-tsoa-ts43-ref-alias If you follow the instructions in the README you should be able to reproduce this issue.

You should also be able to confirm that this issue cannot be reproduced with Typescript 4.1.

Context (Environment)

Version of the library: 3.14.1 Version of NodeJS: 14.15.4

I'm not using yarn but I don't think it matters in this case 😃

ggrossetie avatar Nov 20 '21 11:11 ggrossetie

Hello there Mogztter 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

github-actions[bot] avatar Nov 20 '21 11:11 github-actions[bot]

Same issue with tsoa 4.0.0-rc.1 (Typescript 4.1 and more recent versions)

Lleios avatar Mar 07 '22 13:03 Lleios

I have the same issue here

Toltar avatar Aug 30 '22 16:08 Toltar

I'm curious how this behaves now, TS 4.8 treats this differently apparently

WoH avatar Aug 30 '22 17:08 WoH

Hitting this also with TS4.8.

Craigfis avatar Feb 03 '23 18:02 Craigfis

Getting the same symptom, can't do this for example:

const hello = {
  test: 'whatever'
}

type test = {
  key: typeof hello.test
}

EDIT:

However this seems to work:

const hello = {
  test: 'whatever'
}

type test = {
  key: typeof hello["test"]
}

KennyLindahl avatar Mar 07 '23 10:03 KennyLindahl