-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
🔎 Search Terms
I define a React component MyComponent with a fairly complex prop-type, then I use the component in Example.
The type issue occurs where the type of the parameter item of setItem can no longer be derived as { id: string } | null, instead it is now derived as any which breaks the .id access.
Is this change expected in 5.9? I don't seen anything in the changelog that would cause me to expect this change.
🕗 Version & Regression Information
- This changed between versions 5.8.3 and 5.9.2
⏯ Playground Link
💻 Code
import { useState } from "react";
type Identifiable = { id: string };
interface EnableA {
readonly enableA: true;
readonly enableB: true; // this line seems important
}
interface DisableA {
readonly enableA?: false;
}
interface EnableB {
readonly enableB?: true;
}
interface DisableB {
readonly enableB: false;
}
interface EnableC {
readonly enableC: true;
}
interface DisableC {
readonly enableC?: false;
}
export interface EnableD<I extends Identifiable> {
readonly enableD: true;
readonly value: I["id"] | null;
readonly setItem: (item: I | null) => void | Promise<void>;
}
export interface DisableD<I extends Identifiable> {
readonly enableD: false;
readonly value: I["id"];
readonly setItem: (item: I) => void | Promise<void>;
}
type MyComponentProps<I extends Identifiable> = (EnableA | DisableA) &
(EnableB | DisableB) &
(EnableC | DisableC) &
(DisableD<I> | EnableD<I>);
const MyComponent = <I extends Identifiable>(props: MyComponentProps<I>) => {
return <>{props.value}</>;
};
const Example = () => {
const [ item, setItem ] = useState<string | null>(null);
return <MyComponent
value={item}
setItem={(item) => { // TS 5.8 would correctly derive `{ id: string } | null` as the type of item, but as of TS 5.9 it's 'any'.
if (item) {
setItem(item.id);
} else {
setItem(null);
}
}}
enableD={true}
/>;
};🙁 Actual behavior
setItem={(item /* type is derived as any */) => {
🙂 Expected behavior
setItem={(item /* type is derived as { id: string } | null */) => {
Additional information about the issue
No response
hinstw
Metadata
Metadata
Assignees
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases