-
Notifications
You must be signed in to change notification settings - Fork 12.8k
React props' generic not correctly inferred #41879
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
@weswigham this doesn't look right to me -- the |
Here's a react-less repro for the bot: // Variable type
type Pagination = {
limit: number;
page: number;
};
// Demo Component
type Props<TVariables extends Pagination> = {
variables: TVariables;
setVariables(variables: TVariables): void;
};
function Demo<TVariables extends Pagination>(props: Props<TVariables>) {
return null;
}
// App
type exprType = {
test: string;
limit: 10;
page: 1;
};
export default function App() {
const [variables, setVariables]: [exprType, (arg: exprType | ((p: exprType) => exprType)) => void] = null as any;
return Demo({
variables,
setVariables
});
} |
Nope! Both the |
So.... you could say this is unfortunate but working as intended? We've already "fixed" this - but only for arrow functions, and only in strict mode. I assume we limited inference changes to only those situations for compatibility reasons, for example #27357. |
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in this issue running against the nightly TypeScript. If something changes, I will post a new comment. Comment by @weswigham ❌ Failed: -
Historical InformationComment by @weswigham
|
TypeScript Version: 4.0.3
Search Terms: generic, inference, react, prop, extends
Code
Expected behavior: I expected
TVariables
to be inferred fromvariables
and sosetVariables={setVariables}
to work just fine.Actual behavior:
setVariables={setVariables}
gives the following error :Playground Link: CodeSandbox
Solutions that weirdly work:
I found three solutions that make the issue disappear. So the problem is not fixing the error at all cost, but rather to understand why it's there in the first place.
1- Explicitly setting
TVariables
usingtypeof
works :2- Explicitly narrowing what is passed to
setVariables
makes the code work :3- The weirdest of all : changing the syntax for the
Props
definition by using an alternative syntactical form which I thought equivalent - works :The text was updated successfully, but these errors were encountered: