question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Unknown type: IndexedAccessType

See original GitHub issue

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

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:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

1reaction
relentlessricktrinidadcommented, Nov 23, 2021

Any update on this? We are currently using Prisma to generate our typescript definitions and this issue is preventing us from continuing unfortunately.

0reactions
WoHcommented, Dec 28, 2021

Should be fixed in #1160

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found