question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type inference destructure object

See original GitHub issue

Bug 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

Playground

💻 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:open
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewbranchcommented, Sep 9, 2022

@herrlegno that’s a duplicate of #241 (congrats, you found a three-digit one!)

1reaction
andrewbranchcommented, Sep 9, 2022

#50604 is definitely working as intended. If T is instantiated with an object that also has a property x, the result of Omit<T & { x: X }, "x"> will not be assignable to T.

Likewise, the pattern in the OP here (and my reduced case) should be an error, since T can be instantiated with an object including the legend property (or foo in my reduced case), but the elaboration in the error message is nonsensical:

Type 'Omit<Value & T & { foo: unknown; }, "foo">' is missing the following properties from type 'Value': key, color, value(2322)

This makes me think that it’s an error for a completely incorrect reason.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found