Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

ObjectOmit narrows any type to {} #19

Open
OliverJAsh opened this issue Feb 16, 2018 · 7 comments
Open

ObjectOmit narrows any type to {} #19

OliverJAsh opened this issue Feb 16, 2018 · 7 comments

Comments

@OliverJAsh
Copy link
Contributor

import { ObjectOmit } from 'typelevel-ts';

type OmitUser<T> = ObjectOmit<T, 'user'>;

// Type is `{}`, expected `any`
type Result = OmitUser<any>;

Is there any way around this?

@KiaraGrouwstra
Copy link

hm. when removing string-based keys, doesn't it make sense that the output would be some kind of object?

@OliverJAsh
Copy link
Contributor Author

I'm not sure 🤔

For context, this behaviour is causing issues for me when I'm trying to use the withDefaults HOC, where the composed component doesn't have its prop types defined yet:

class Foo extends React.Component<any> {}

const DefaultedFoo = withDefaults(Foo, { baz: 1 });

// Unexpected error: Property 'bar' does not exist on type 'IntrinsicAttributes & Pick<any, never> & Partial<{ baz: number; }> & { children?: ReactNode; }'.
<DefaultedFoo bar="foo" />;

This is because ObjectDiff uses ObjectOmit, and in this case it resolves to Pick<any, never>.

Although I am able to workaround the issue by widening the default props object to any:

type Props = any;
class Foo extends React.Component<Props> {}

const defaults: Props = { baz: 1 }
const DefaultedFoo = withDefaults(Foo, defaults);

<DefaultedFoo bar="foo" />;

@KiaraGrouwstra
Copy link

Makes sense. {} admittedly is rather restrictive (no keys known to exist), whereas the intent in any was obviously to make it less restrictive, which seems to backfire now. Guess that does seem unfortunate.

@amir-arad
Copy link

I think design decisions in typescript made a turn for the worse :(
microsoft/TypeScript#20247

@IanYates
Copy link

Would Conditional Types - microsoft/TypeScript#21316 - help here?

@OliverJAsh
Copy link
Contributor Author

@IanYates Maybe! Could we do something like type OmitUser<user> = T extends any ? any : ObjectOmit<T, 'user'>?

@KiaraGrouwstra
Copy link

I fear all types would extend any -- in that event I'd suggest flipping the check to any extends T ? -- I suppose that would actually hold only if T were to be any as well.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants