Repository navigation
[4.1.0-beta] Incorrect method overload selected #41099
Description
Relevant investigation data:
Material UI makeStyle function has 2 signatures, 1 where Props are empty object ({}), the second where props are a generic (default of {}). When using the spread operator for the object passed into makeStyle, the second more specific (and incorrect) signature is picked. This ONLY happens when there are other values provided before or after the spread operator. If the spread object is the only set of values in the object, the 'correct' overload is suggested.
I believe all 3 cases below should be equivalent and select the same function overload. This works perfectly in previous versions of typescript, so would indicate some form of bug or un-tracked breaking change.
TypeScript Version: 4.1.0-beta & 4.1.0-dev.20201014
Search Terms:
Material UI styles TS2554 overload type
Code
const useStylesBroken = makeStyles(theme =>
createStyles({
sizeSmall: {
...theme.typography.button, // This is simply a set of css styles - nothing relating to props
height: '36px',
},
})
);
const useStylesWorks = makeStyles(theme =>
createStyles({
sizeSmall: {
height: '36px',
},
})
);
const useStylesWorks2 = makeStyles(theme =>
createStyles({
sizeSmall: {
...theme.typography.button,
},
})
);
export function NotReallyAButton(): null {
console.log(useStylesBroken());
console.log(useStylesWorks());
console.log(useStylesWorks2());
return null
}
See https://github.com/berickson1/Playground/blob/master/materialUiSample.ts
Expected behavior:
Code compiles successfully (as in previous released typescript versions)
Actual behavior:
Incorrect overload chosen & compiler error:
materialUiSample.ts:27:17 - error TS2554: Expected 1 arguments, but got 0.
27 console.log(useStylesBroken());
~~~~~~~~~~~~~~~~~
node_modules/@material-ui/core/styles/makeStyles.d.ts:23:5
23 ): (props: Props) => ClassNameMap<ClassKey>;
~~~~~~~~~~~~
An argument for 'props' was not provided.
Playground Link:
Requires material-ui dependency installed, see repo at https://github.com/berickson1/Playground (yarn install && yarn build)
Related Issues:
#41049 <- Looks similar but only relates to promises, so is not applicable here
Activity
RyanCavanaugh commentedon Oct 16, 2020
Brent Erickson (@berickson1) is there a test suite or something I can use when testing out modifications to the material-ui definitions to make sure the things I might propose don't break anything?
RyanCavanaugh commentedon Oct 16, 2020
Bisected to #40002
Without an isolated repro I don't see us getting to this right away - my initial attempt to distill this down into something that didn't require the material-ui definitions wasn't successful
RyanCavanaugh commentedon Oct 16, 2020
In the meantime I changed this
to this
and got the desired behavior
berickson1 commentedon Oct 16, 2020
What I've shared is about as far as I've gotten to be honest. I was trying to prep our client codebase for typescript 4.1 and deal with the latest set of changes and ran into this. I'm only familiar with MUI types from a usage standpoint, but I can try to distill a specific example down either this weekend or early next week.
I largely wanted to get this on the radar for the TS team since MUI is a relatively popular UI component library and this introduces an (albeit unintentional) breaking change.
berickson1 commentedon Oct 16, 2020
I did some digging and tracked the behaviour back to 1 specific line in the material-ui .d.ts. Commenting it out fixes everything in my example (but it's still needed for some font face overrides in general).
Below is my (somewhat stream of consciousness) dive into the underlying MUI types a bit - may or may not be useful for investigations, but doesn't hurt to post
A bit more info:
createStyletakes 2 generics, the 2nd being Props.This uses
The problematic type in there is
CreateCSSProperties<Props>. This defines a type with two index accessors - one from extendingBaseCreateCSSProperties, the other defined inline. Removing either of these index signatures makes the compiler happy.This is about as far as I've been able to make it today.
berickson1 commentedon Oct 23, 2020
After a short delay, I have a contrived standalone repro. I've updated the git repo above and created a playground link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAUgynAYgewHbAGYEMDG0C8UA3gFBRQD0AVFAM7IC20YATspC8AJYS1QAWEFhAA0UACbJeUVMmBQGWYMCFQMyFlGFhkUKhRIBfANwkSoSFAAKbMLUQBXVDgA8N9nwgAPFanF9kACMAKwgcYDEAFQA+KEIAClYPAC5rW1oASjjYyNMSaioyGgBBABtS5AB3PmBBKAdaVWBdLAcAcyZ0KFrmWyFuaSwANywuUqxA0ogigy50IWw8KDghtvcOAdoXSIAZCFQ22rioeNpgFjm2qAAyYkMsgB8oAAZY0nJArEbSuYg4fi4GGAAH5Urt9od+KZyBgxioWKC6OdLjc7qZDCRvDpOFA5vDFtB1v0eFtwQcjgkzhcDqiiPcoE9XmQoN5fP5lqsiZwSds9uT+G8MSQ8QtcNAAEJfCAAYQQXM2LJ8+3Z8p5qAcDECqieVMub2ZFAoADkAPKRACi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3Wg8Q+zfgAF5-EUOUVIfZyBESQdgE+Z8L5XxvnfMQLEyJOSct+X0klPxTnkMTHAtA+BnFrtIWMMg5AAhpMIcY3AaRGSSHYWmggfz8GAKkAARAAZgAGxgC8Hw325BDC+3uKYL+7lPKXDvgAJkAdXUB4CvZcGgXAWBpR4G0yQURFBEBT46HQdfW+7dsEDlwQ6ShXBCFSTXI0UhtpyGUNAVAWhL4GFXCYXxVhuh2G0GZFIowGRZFoEPh5Fgh5FHt1UcAu+YCIFaJgRJfR6suFtB4fw4RojxGhOkRE8epF1K0CrlwMA8hn5EWeJ+C+oUaj8CUHaXg0AsAoQgJuN8Xi1KQAqRcapUBakAEZAKMRYPOCQgJJk8SgDHLcpkNpemEG7cQYgIBRRWfwCCOS2lSVoLsrqxloCBl9HxSY0wcHkScmuQIYFTK2jXPvZEUVCreB9H6UQxkHJgXDHwFcZwxhTEKpIU8ag2AMGIYdUoCgjoXF6Q4LgRUICxnEo0PeUT5BYlusuO+ozUi0WJjEuJBwAGEB-sgP+d9IkHxxV4SA4R8XtyUUS9SJKFHkoSYQUl8TqFKNMEAA
berickson1 commentedon Oct 23, 2020
The title here should be updated - this doesn't appear to be related to method overloads based on the distilled sample. It appears that inferred generics are improperly selected due to the usage of the spread operator in object declaration.
danvk commentedon Nov 3, 2020
I also ran into this issue today when I tried out the TS 4.1 RC. In case it's helpful, here's my repro. This type checks in TS 4.0.2 but not 4.1RC.
timbuckley commentedon Nov 20, 2020
This bug persists on
4.1.21 remaining item
ahejlsberg commentedon Sep 27, 2023
I'm going to close this issue. The repro is quite complex and it isn't clear to me what the types are trying to achieve, plus it looks like the working cases in the repro actually rely on type inference failing and defaulting to the
{}constraint.