Error: Unknown type: IndexedAccessType
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
Expected Behavior
The problem already was discussed in #862. Type resolver throws this error when you use a type like this:
export const MessageState: {
pending: 'pending',
available: 'available'
};
export type MessageState = (typeof MessageState)[keyof typeof MessageState]
Generate routes error.
Error: Unknown type: IndexedAccessType
At: controllers/Controller.ts:16:16.
This was caused by 'type MessageState = (typeof MessageState)[keyof typeof MessageState]'
at new GenerateMetadataError (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/exceptions.ts:6:5)
at TypeResolver.resolve (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:318:13)
at TypeResolver.getTypeAliasReference (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:617:120)
at TypeResolver.getReferenceType (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:585:30)
at TypeResolver.resolve (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:359:32)
at /my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:107:127
at Array.reduce (<anonymous>)
at TypeResolver.resolve (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:106:10)
at TypeResolver.getTypeAliasReference (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:617:120)
at TypeResolver.getReferenceType (/my_project/node_modules/@tsoa/cli/src/metadataGeneration/typeResolver.ts:585:30)
Possible Solution
Solution suggested in #862:
// file: packages/cli/src/metadataGeneration/typeResolver.ts
// ↓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)) {
...
It resolves this particular case but doesn’t resolve the case when using different object types with the same indexes:
const a = {
a: 1,
d: 4
} as const
const b = {
a: "a",
d: "d"
} as const
type Works = keyof typeof b // "a" | "d"
type Works = (typeof b)[keyof typeof b] // "a" | "d"
type NotWorks = (typeof a)[keyof typeof b] // 1 | 4
Context (Environment)
Version of the library: 3.14.0 Version of NodeJS: 14.13.1
- Confirm you were using yarn not npm: [x]
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:6
Top Results From Across the Web
Unknown type: IndexedAccessType. · Issue #338 · lukeautry ...
I would like to be able to set controller return type from ... Generate routes error. ... Error: Unknown type: IndexedAccessType. at new ......
Read more >Documentation - Indexed Access Types - TypeScript
We can use an indexed access type to look up a specific property on another ... You'll even see an error if you...
Read more >Typescript thinks indexed access type T[EventKeys<T>] isn't a ...
The problem is, Typescript doesn't realise that the indexed access type T[EventKeys<T>] must be a MyEvent . Even using a type assertion ...
Read more >Use the TypeScript "unknown" type to avoid runtime errors
The "unknown" type is useful when building services or APIs that can return data of an unknown shape, so we might want to...
Read more >Types from Extraction
type Predicate = (x: unknown) => boolean; type K = ReturnType<Predicate>; ... If we try to use ReturnType on a function name, we...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any update on this? We are currently using Prisma to generate our typescript definitions and this issue is preventing us from continuing unfortunately.
Should be fixed in #1160