Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Couldn't resolve literal node -1 (type literals for negative numbers) #1733

Open
2 of 4 tasks
crycode-de opened this issue Dec 19, 2024 · 2 comments · May be fixed by #1734
Open
2 of 4 tasks

Error: Couldn't resolve literal node -1 (type literals for negative numbers) #1733

crycode-de opened this issue Dec 19, 2024 · 2 comments · May be fixed by #1734

Comments

@crycode-de
Copy link

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

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

Expected Behavior

Type literals in an interface used as response should be able to include negative numbers.

Simple example controller:

import { Controller, Get, Route } from 'tsoa';

interface TestResponse {
  value: -1 | 0 | 1 | 2;
}

@Route('test')
export class TestController extends Controller {

  @Get('/')
  public getIndex (): TestResponse {
    return {
      value: -1,
    };
  }
}

Current Behavior

The above example fails to build with

There was a problem resolving type of 'TestResponse'.

GenerateMetadataError: Couldn't resolve literal node: -1
    at TypeResolver.getLiteralValue (/<workspace>/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:514:85)

Without the -1 in the type literal the build succeeds.

Possible Solution

The Problem is in the getLiteralValue method:

private getLiteralValue(typeNode: ts.LiteralTypeNode): string | number | boolean | null {
switch (typeNode.literal.kind) {
case ts.SyntaxKind.TrueKeyword:
return true;
case ts.SyntaxKind.FalseKeyword:
return false;
case ts.SyntaxKind.StringLiteral:
return typeNode.literal.text;
case ts.SyntaxKind.NumericLiteral:
return parseFloat(typeNode.literal.text);
case ts.SyntaxKind.NullKeyword:
return null;
default:
throwUnless(Object.prototype.hasOwnProperty.call(typeNode.literal, 'text'), new GenerateMetadataError(`Couldn't resolve literal node: ${typeNode.literal.getText()}`));
return (typeNode.literal as ts.LiteralExpression).text;
}
}

For the literal -1 the typeNode.literal.kind is ts.SyntaxKind.PrefixUnaryExpression.

Adding the following code to the switch will fix the issue:

      case ts.SyntaxKind.PrefixUnaryExpression:
        return parseFloat(typeNode.literal.getText());

Steps to Reproduce

See example above.

Context (Environment)

Version of the library: 6.6.0
Version of NodeJS: 20.16.0

  • Confirm you were using yarn not npm: [ ]

Detailed Description

Breaking change?

I don't think so.

Copy link

Hello there crycode-de 👋

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

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

crycode-de added a commit to crycode-de/tsoa that referenced this issue Dec 20, 2024
Also added negativeNumberLiteralType to the tests
Fixes lukeautry#1733
@crycode-de crycode-de linked a pull request Dec 20, 2024 that will close this issue
5 tasks
@crycode-de
Copy link
Author

Created PR #1734 to fix this issue including additions to the testModel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant