TypeScript: Nightly bug(?) with infering function generic while being part of intersection

First of all, the title exists for the sake of a title. I don’t really know exactly what is wrong with this bug. I came across this while working on something with redux tookit. Screen Shot 2022-05-30 at 2 15 00 AM Error: Parameter 'state' implicitly has an 'any' type. Expected: state = WritableDraft<{username: string, isLoggedIn: true, ...}>

I am using the nightly extension and the bug has not occured because of an update to redux toolkit or a change in my code, but has instead been because of typescript versions changing. The code itself works in typescript 4.7.0 but not in typescript nightly/4.8.0

In order to create a minimum reproducible example, I have tried to simplify it as much as possible, (which was harder than I imagined). The problem is rather weird, there are multiple parts that cause this bug and removing any will fix it. However, the issue remains, version 4.7.0 works while the same on nightly errors.

At this point, I still do not know exactly what the problem is. In fact, it may even be intentional. However, this is something that breaks redux toolkit so I think the matter is something worth taking a look at.

What search terms have I used? None. I don’t know how to put this bug into words.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 27 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Taking possible red herrings out of the repro:

// @strict: true

declare function createSlice<T>(
    reducers: { [K: string]: (state: string) => void } & { [K in keyof T]: object }
): void;

createSlice(
	{
		f(a) {}
	}
)

a is string in 4.7 and implicit any in 4.8 and I don’t see a good reason for that to have changed.

Yeah. We have both unit tests, and a set of “type tests”.

to run them:

cd packages/toolkit
# Run just the createSlice unit tests
yarn test createSlice.test 
# Run _all_ the typetests
yarn type-tests

Hi, I’m another Redux Toolkit maintainer. Wanted to follow up on this.

Lenz has put together a potential workaround on our side, but it requires what is frankly a pretty ugly hack (a separate package that abuses typesVersions to figure out what TS version we’re dealing with, and changing what our createSlice type includes based on that).

Any thoughts on whether this issue is something the TS team intends to address before 4.8 is released?

The change between v4.7.4 and main occurred at 9236e39374c0ec9a1e3f9894af4fb9eb34ba0021.