Skip to content

Generics in conditionals have differing behavior in 4.8 #49490

Open
@DanielRosenwasser

Description

@DanielRosenwasser

I found a break in zustand from looking at the logs of a failed NewErrors run. It might be more correct, but figured it was worth noting.

export type Store<T extends object> = {
    setState: (x: T) => void
    getState: () => T;
}

type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type Extractable<S extends Store<object>> = {
    getServerState?: () => ExtractState<S>
}

export function f<TState extends object>(api: Extractable<Store<TState>>) {
//                                                        ~~~~~~~~~~~~~
// Type 'Store<TState>' does not satisfy the constraint 'Store<object>'.
//   Types of property 'setState' are incompatible.
//     Type '(x: TState) => void' is not assignable to type '(x: object) => void'.
//       Types of parameters 'x' and 'x' are incompatible.
//         Type 'object' is not assignable to type 'TState'.
//           'object' is assignable to the constraint of type 'TState', but 'TState' could be instantiated with a different subtype of constraint 'object'.
}

https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L30

export function useStore<TState extends State, StateSlice>(
  api: WithReact<StoreApi<TState>>,
//               ~~~~~~~~~~~~~~~~
// error TS2344: Type 'StoreApi<TState>' does not satisfy the constraint 'StoreApi<object>'.

There's a few of these.

New errors for non-composite project https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tsconfig.json

Originally posted by @DanielRosenwasser in #49488 (comment)

Metadata

Metadata

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions