Skip to content

Recursive type definitions regression in 3.4.0 #30841

Closed

Description

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Recursive
Conditional

Code

type Action<
  Args extends any[],
  Payload extends Record<string, any>,
  T extends string
> = ((...args: Args) => (Payload & { type: T })) & {
  readonly type: T
}

type AnyAction = Action<any[], any, string>

type CascadingActionMap = {
  [name: string]: AnyAction | CascadingActionMap
}

type Reducer<S, T> = (state: S, action: T) => S

type CascadingActionType<T> = T extends CascadingActionMap
  ? { [K in keyof T]: ActionType<T[K]> }[keyof T]
  : never

export type ActionType<T> = T extends AnyAction
  ? ReturnType<T>
  : T extends Reducer<any, infer A>
  ? A
  : CascadingActionType<T>

Expected behavior:
No errors. It's worth noting that it works in 3.3.4, with the correct derived type.

Actual behavior:
Running tsc command line, it reports the following errors:

error TS2456: Type alias 'CascadingActionType' circularly references itself.

914 type CascadingActionType<T> = T extends CascadingActionMap
         ~~~~~~~~~~~~~~~~~~~

error TS2315: Type 'ActionType' is not generic.

915   ? { [K in keyof T]: ActionType<T[K]> }[keyof T]
                          ~~~~~~~~~~~~~~~~

error TS2456: Type alias 'ActionType' circularly references itself.

918 export type ActionType<T> = T extends AnyAction
                ~~~~~~~~~~

error TS2315: Type 'CascadingActionType' is not generic.

924   : CascadingActionType<T>
        ~~~~~~~~~~~~~~~~~~~~~~

Playground Link:

Related Issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions