question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generics in conditionals have differing behavior in 4.8

See original GitHub issue

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 https://github.com/microsoft/TypeScript/issues/49488#issuecomment-1152817431_

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Jun 13, 2022

This is quite complex and beyond my understanding. We can already observe the issue with type here. However, the same behavior can be observed on 4.7 with this and if we change type to an interface then we might see an error on the variance annotation, see here.

In Zustand repo I couldn’t see the same annotation error though so I kept digging… and I’ve somewhat narrowed it down to this. In here we won’t observe the variance annotation error but it’s quite easy to get it - by removing some seamingly unrelated lines of code.

The code here has some cycles and stuff and I think that this just makes variance measurement to give up and thus it ends up too forgiving. For some reason though the type-based variant was accepted in the past and now it isn’t. It looks a little bit like as if variance computation became slightly smarter now and errors sooner rather than reporting unmeasurable (or similar)

Note that I’m just guessing a lot here.

0reactions
devanshjcommented, Jun 14, 2022

Hm, not sure how I made that slip-up, and I’m honestly a little confused about the interface/type alias difference. If you have some time to get a minimal repro that’d be at the very least useful for us to investigate.

Yeah I too am not sure what’s up with the interface vs type alias thing. Will try to get a minimal repro if I find time for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Conditional Types - TypeScript
JavaScript programs are no different, but given the fact that values can be ... But the power of conditional types comes from using...
Read more >
The Definitive TypeScript 4.8 Guide - SitePen
TypeScript is unique in that it is a superset of JavaScript, but with optional types, interfaces, generics, and more.
Read more >
Comparison of generic-to-brand switchback patterns ... - NCBI
While generic drugs are therapeutically equivalent to brand drugs, ... that generic drugs have different side effects than branded drugs.
Read more >
Generic Methods - Java™ Tutorials
Notice that we don't have to pass an actual type argument to a generic method. The compiler infers the type argument for us,...
Read more >
VHDL 2019: Conditional Analysis - Insights - Sigasi
In all these cases, to get similar behavior it's possible to find a proper approach for each tool, but it requires using different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found