Generate routes error (IndexedAccessType + TypeOperator)
See original GitHub issueSorting
-
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
Current Behavior
This is a problem similar to the Enum problem that was improved with this pull request. https://github.com/lukeautry/tsoa/pull/636
For example, if you use the KeyOfKeyword
type generated using the object’s key value as the schema, the Generate routes error will occur.
const genderCds: {
readonly male: "male";
readonly female: "female";
};
type GenderCd = typeof genderCds[keyof typeof genderCds];
export interface TestModel {
genderCd: GenderCd;
}
Generate routes error.
Error: Unknown type: IndexedAccessType
At: /mypath/to/xxx/index.d.ts:8:8.
This was caused by 'type GenderCd = typeof genderCds[keyof typeof genderCds];'
at new GenerateMetadataError (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/exceptions.js:22:28)
at TypeResolver.resolve (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:308:19)
at TypeResolver.getTypeAliasReference (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:529:353)
at TypeResolver.getReferenceType (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:505:38)
at TypeResolver.resolve (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:341:34)
at TypeResolver.resolve (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:294:131)
at TypeResolver.propertyFromSignature (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:761:143)
at /mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:727:161
at Array.map (<anonymous>)
at TypeResolver.getModelProperties (/mypath/to/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:727:124)
Possible Solution
When parsing a type like GenderCd
in this example with the TypeResolver class,
ts.isIndexedAccessTypeNode(this.typeNode)
is true
and
ts.isTypeOperatorNode(this.typeNode.indexType) && this.typeNode.indexType.operator === ts.SyntaxKind.KeyOfKeyword
is true.
Therefore, I confirmed that it works as expected by resolving indexType as a actual type as shown below.
// ↓added
if (ts.isIndexedAccessTypeNode(this.typeNode)) {
return new TypeResolver(this.typeNode.indexType, this.current, this.typeNode, this.context, this.referencer).resolve();
}
// ↑added
if (ts.isParenthesizedTypeNode(this.typeNode)) {
...
However, I have no experience with TypeScript type analysis, so I’m not sure if this solution is correct. Is it possible to consider a complete solution?
It’s very useful to dynamically type string literals, so I’d be very happy if this could be resolved.
Thank you.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top GitHub Comments
I am running into this exact same problem with tsoa 3.9.0 when using enums in Prisma. Prisma generates types that look somewhat like this:
which produces the same error when put into a model:
I can confirm that the possible solution provided by @m-doi2 gets rid of the error and seems to produce correct routes and specs for the simple example above.
It would be great if this issue could be reopened and reevaluated.
Thank you!
I’m having the same issues on the latest version (v.3.14.1) as well. I also use Prisma which generates enum types like described above:
And it gives me the same
Error: Unknown type: IndexedAccessType
error.I tested it using #799 version and it also produces the same errors.
And I could also confirm what the fix suggested by @m-doi2:
resolves the issues and produces correct types. And all tests are passed, so it doesn’t seems to break anything.
I can create a PR with suggested fix. @WoH can you please take a look if this is a correct solution?