Skip to content

Regression in types after v5.5Β #59108

Closed
@Titozzz

Description

@Titozzz

πŸ”Ž Search Terms

cannot be used to index type
Generics
forwardRef

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.2#code/CYUwxgNghgTiAEYD2A7AzgF3gMwFzwB4AVAPgAo94yAPfIgSngF4T4BXFAaxSQHcVGLKrXgNmrDtz4oAUAHo58ALZROASxQBzeBgAWatPCjw4UCPDWgUGNRgCeODmBup4ICGqUaoGEIb0IIDAwSDC48oqgkLAIyOhYlMTkImJCRDIyUdBwiKiY8Jr4xG7UvijAhpI8-Mn4AN44SEh08AC+ghJc1bIKJiBKSABuGtoBBSAoQWpgufEwUBpY7p7evv66gcGh4b1ZMbP5hYSkNPWNzaJtHexd0hlx+brMOGSa9BElAA4eYLYQDrwYLYRvAkGwsGMQNQoM4dHZPgg0LppBYUHCEajsEg3B4vCgfH4dBs3Fswh89jkHlgoEUiCUyhUblIaqd4A0sRc6e1xEzuhl7BiAGJqGCYAAKsCgShAvhgSWedKhDMMrI02CC8DF1yqKIA-Jr4PgdfwProoMMtETAhA0AgAEbzFBgJ5sW3wAAGk0GQXdOJW+LWVpJITJvQFCGFoowEvm0tl8qYlyVE0ZqpQ6pgmu1t348H1YsN8C9QX58IQAA1npHxZK40ECOGkNh4LoSABtADkHI7AF0ZEA

πŸ’» Code

declare const f: <T>(f: (x: T) => unknown) => (x: T) => unknown
// making this a real identity function eliminates the error:
// declare const f: <T>(x: T) => T

declare const g: <T extends unknown>(x: { foo: T }) => unknown
// removing the generic constraint eliminates the error:
// declare const g: <T>(x: { foo: T }) => unknown

const h = f(g)
// explicitly writing out the exact type shown in type info eliminates the error:
// declare const a: <T extends unknown>(x: { foo: T }) => unknown

type FirstParameter<T> = T extends (x: infer P) => unknown ? P : unknown
// having the else branch use `never` eliminates the error:
// type FirstParameter<T> = T extends (x: infer P) => unknown ? P : never

type X = FirstParameter<typeof h>['foo']

πŸ™ Actual behavior

Starting after said commit, this errors with: Type '"foo"' cannot be used to index type 'FirstParameter<(<T extends unknown>(x: { foo: T; }) => unknown)>'.

πŸ™‚ Expected behavior

No error, and types inferred as before

Additional information about the issue

This was initially detected with a more complex example, you can find it here on TS discord https://discord.com/channels/508357248330760243/1255537732730491012/1255537732730491012 but was narrowed down thanks to @mkantor.
I already pinged the initial author @Andarist and he started looking at it.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions