Skip to content
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

Type {} is not assignable to type PartialDeep<T> #916

Open
tommy-mitchell opened this issue Jul 15, 2024 · 1 comment
Open

Type {} is not assignable to type PartialDeep<T> #916

tommy-mitchell opened this issue Jul 15, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tommy-mitchell
Copy link
Contributor

tommy-mitchell commented Jul 15, 2024

Combining PartialDeep with a generic does not accept an empty object initializer:

import type {PartialDeep, UnknownRecord} from 'type-fest';

type Person = {
  name: string;
  age: number;
};

const mock = <T extends UnknownRecord>(mock: PartialDeep<T> = {}): T => mock as T;
//                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
//=> Error (ts2322): Type `{}` is not assignable to type `PartialDeep<T>`

const person = mock<Person>({name: 'Bob'});
//    ^? const person: Person

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@Max10240
Copy link
Contributor

Max10240 commented Jul 15, 2024

Just my own opinion, it probably has nothing to do with PartialDeep, A simpler example is:

// Type 'string' is not assignable to type 'T'.
//   'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.(2322)
const func = <T extends string>(value: T = '') => value;

Because T can be any subtype of UnknownRecord, it could even be never; And if T is never, then mock: PartialDeep<T> is never, in this case {} cannot be assigned to never, so TS made such a complaint.

In addition, the following code is error-free:

import type {PartialDeep, UnknownRecord} from 'type-fest';

// no error here, so "{} is assignable to type PartialDeep<T>"
const foo: PartialDeep<UnknownRecord> = {};

@fregante fregante added the bug Something isn't working label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants