Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer interdependencies in object literals #41396

Closed
gffuma opened this issue Nov 4, 2020 · 1 comment
Closed

Infer interdependencies in object literals #41396

gffuma opened this issue Nov 4, 2020 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@gffuma
Copy link

gffuma commented Nov 4, 2020

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:

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 12, 2020
@RyanCavanaugh
Copy link
Member

See #30134 - certain parts of inference depend on left-to-right per-parameter inference; we would need unification to infer this correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants