-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generics in conditionals have differing behavior in 4.8 #49490
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
Comments
TypeScript 4.7 is producing the same error. And I remember hitting it very frequently in my own project, which requires me to write |
cc @devanshj - you might find this interesting |
Thanks for the ping @Andarist! Will take a look into it soon but for the time being I want to point out that @DanielRosenwasser's reproduction is incorrect because zustands's type Store<T extends object> = {
getState: GetState<T>
setState: SetState<T>
}
type GetState<T> = () => T
type SetState<T> = { _(x: T): void }["_"]
type ExtractState<S> = S extends { getState: () => infer T } ? T : never
type Extractable<S extends Store<object>> = {
getServerState?: () => ExtractState<S>
}
function f<TState extends object>(api: Extractable<Store<TState>>) {} And this compiles in both 4.7 and 4.8. So the errors being reported (which I haven't looked into yet) seem to be caused by some other reason. |
From pmndrs/zustand#1004
|
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. |
This is quite complex and beyond my understanding. We can already observe the issue with 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. |
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. |
This SO question is running into this. |
I see there has been some requests for a minimal reproduction. I think the code used in my SO question (linked by @jcalz ) might be able to provide that: interface Vehicle {}
interface Workshop<TVehicle extends Vehicle = Vehicle> {
vehicleType: TVehicle;
repair: (vehicle: TVehicle) => WorkOrder<this>;
}
// Unexpected `Type 'TWorkshop["vehicleType"]' does not satisfy the constraint 'Vehicle'.ts(2344)`
interface WorkOrder<TWorkshop extends Workshop<TWorkshop["vehicleType"]>> {
vehicle: TWorkshop["vehicleType"];
} Here's a playground link which includes objects proving that the Indeed, with TS 4.7 there are no (unexpected) errors but in 4.8 there is. |
The one above bisects to 4.8.0-dev.20220528 and more specifically to removing those lines: |
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.
https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L30
There's a few of these.
New errors for non-composite project https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tsconfig.json
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L30
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L56
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L59
TS2345 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/src/react.ts#L68
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/context.test.tsx#L24
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/context.test.tsx#L48
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/context.test.tsx#L135
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/context.test.tsx#L150
TS2322 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/middlewareTypes.test.tsx#L171
TS2344 at https://github.com/pmndrs/zustand/blob/197e5d41993b0e3ad6ebef9a7e6a9161753072f1/tests/types.test.tsx#L86
Originally posted by @DanielRosenwasser in #49488 (comment)
The text was updated successfully, but these errors were encountered: