Type inference destructure object
See original GitHub issueBug Report
Typescript seems to not be able to infer correctly a rest object type.
I’ve found a workaround for it by nesting the value (in playground)
🔎 Search Terms
Destructuring
🕗 Version & Regression Information
Tested in 4.5.5, 4.8.2 and next (time of writing = v4.9.0-dev.20220908)
⏯ Playground Link
💻 Code
type Value = { // This properties should be always inside the dataset, to be able to render the graph
key: string;
color: string;
value: number;
};
export type GraphDataSetType<
T extends Record<string, unknown> = Record<string, never> // This represents extra data that can be used when rendering the legend for a value
> = (
(Value & T) & {
legend: JSXElementConstructor<Value & T>
})[];
type Props<T extends Record<string, unknown> = Record<string, never>> = {
values: GraphDataSetType<T>;
};
/* This doesn't work */
const Component = <T extends Record<string, unknown>>({values}: Props<T>) => values.map(({ legend: CustomLegend, ...props }) => <CustomLegend {...props} />)
// ^ Error
🙁 Actual behavior
Throws an error
Type 'Omit<Value & T & { legend: JSXElementConstructor<Value & T>; }, "legend">' is not assignable to type 'IntrinsicAttributes & Value & T'.
Type 'Omit<Value & T & { legend: JSXElementConstructor<Value & T>; }, "legend">' is missing the following properties from type 'Value': key, color, value
🙂 Expected behavior
Expect to not throw an error as Omit<Value & T & { legend: ... }, "legend" }> === Value & T
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
typescript - Types in object destructuring - Stack Overflow
Types don't need to be specified for object properties because they are inferred from destructured object. Considering that bar was typed properly, foo...
Read more >Specify TypeScript Types for Destructured Object Properties
Quick overview of how you can add types to destructured object properties using TypeScript.
Read more >TypeScript: Object Destructuring - DEV Community
Giving types for destructured object properties is not very common. Usually, the inference system is reliable and assigns correct types for ...
Read more >Type Inference - TypeScript Deep Dive - Gitbook
And if the function parameter can be inferred, so can its destructured properties. For example here we destructure the argument into its a...
Read more >Destructuring assignment - JavaScript - MDN Web Docs
For both object and array destructuring, there are two kinds of destructuring patterns: binding pattern and assignment pattern, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@herrlegno that’s a duplicate of #241 (congrats, you found a three-digit one!)
#50604 is definitely working as intended. If
T
is instantiated with an object that also has a propertyx
, the result ofOmit<T & { x: X }, "x">
will not be assignable toT
.Likewise, the pattern in the OP here (and my reduced case) should be an error, since
T
can be instantiated with an object including thelegend
property (orfoo
in my reduced case), but the elaboration in the error message is nonsensical:This makes me think that it’s an error for a completely incorrect reason.