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

Add support for typeof keyword in type parameters #1753

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pclements12
Copy link
Contributor

@pclements12 pclements12 commented Mar 3, 2025

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue? Support zod's infer type #1256

References Issue

This attempts to fix at least one of the sub issues related to the ability to use Zod's z.infer that was identified in #1256
#1256 (comment)

  1. Typeof Name Calculation Issue
    Error message: No matching model found for referenced type value.
@Route("/test")
export class TSOATestController extends Controller {
  @Get("/")
  async test(): Promise<ValueType> {
    return null as unknown as ValueType;
  }
}

const value = null;

type Infer<T> = T;

type ValueType = Infer<typeof value>;

The issue is that when calcTypeName runs for a typeof operator type (a TypeQueryNode), it calls calcRefTypeName on the operand. The doc comment and code for calcRefTypeName indicate it only accepts a reference to a type. The obvious problem here is that the operand of typeof is never going to reference a type. The typescript typeof operator only accepts references to javascript variables, functions, etc. – i.e., non-types.

Thus, the call to this.calcRefTypeName(arg.exprName) is simply wrong, and is an outright bug that needs to be fixed.

Test plan

Adds a test case to the metadata / parameter resolver for typeof types as a parameter or return type

Adds an (expensive) unit test for TypeResolver to explicitly enumerate all of the type combinations that the resolver supports in a clean room environment. Open to suggestions on making this more efficient by combining into a single virtual source file, but the context of type right next to test is very valuable imo.

I've also included some types in this test that still fail (currently skipped) and are related to the other issues identified in #1256

@pclements12 pclements12 changed the title Add support for typeof keyword in types Add support for typeof keyword in type parameters Mar 3, 2025
@pclements12 pclements12 mentioned this pull request Mar 4, 2025
4 tasks
@pclements12
Copy link
Contributor Author

@WoH can you take a look at this when you get a chance?

@WoH
Copy link
Collaborator

WoH commented Mar 7, 2025

I'll get to it over the weekend!

It looks good from slimming the code, but I'll try to play around with it a bit

@@ -0,0 +1,512 @@
import { expect } from 'chai';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if you could integrate the tests in this file with the existing tests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec Generators Tests most likely

};

const typeProperties: ts.Symbol[] = this.current.typeChecker.getPropertiesOfType(this.current.typeChecker.getTypeAtLocation(initializer));
const properties: Tsoa.Property[] = typeProperties
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the property management should already exist in checker based resolutions.

Ideally we can merge them together.

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 this pull request may close these issues.

3 participants