Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Infer interdependencies in object literals #41396

Copy link
Copy link
@gffuma

Description

@gffuma
Issue body actions

TypeScript Version: 4.0.5

Search Terms:

infer, object literals, interdependencies

Code

type Selector<S> = (state: S) => any

export interface Reducer<T = any> {
  (state: T | undefined, action: any): T
}

type UnboxState<R extends Reducer> = R extends Reducer<infer U> ? U : never

declare function example<R extends Reducer, S extends Selector<UnboxState<R>>>(r: R, s: S) : void

example(
    (state, action) => ({
        name: 'Gio Va'
    }),
    state => 23
)

declare function example2<R extends Reducer, S extends Selector<UnboxState<R>>>(x: { r: R, s: S }) : void

example2({
    r: (state, action) => ({
        name: 'Gio Va'
    }),
    s: state => 23
})

Expected behavior:

Infer type { name: string } to state in the example2 function as it does correctly in example function.

Actual behavior:

In the example2 function the type inferred to state is any.

Playground Link:

playground

Related Issues:

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.