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

DeepPartial typing does not play nicely with branded types #4235

Closed
flakey-bit opened this issue Dec 12, 2021 · 2 comments
Closed

DeepPartial typing does not play nicely with branded types #4235

flakey-bit opened this issue Dec 12, 2021 · 2 comments

Comments

@flakey-bit
Copy link

flakey-bit commented Dec 12, 2021

Prior Issues

What is the current behavior?

Assume some simple branded type, for example

type Money = string & { __brand: "Money" };

Assume some type, e.g. Transaction that makes use of the branded type in a property, for example

export interface Transaction {
  amount: Money;
  postedDateTime: string;
}

For a given type T, and an instance variable declared as of type DeepPartial<T>, an instance of T should be assignable to the instance variable of type DeepPartial<T>, but with the current implementation this is violated.

The problem is an overly simplisitic DeepPartial definition, there are better implementations out there. NB: I believe the implementation prior to #3369 did not have this problem (but obviously that PR addressed other issues).

Steps to Reproduce

// Money is a "branded" string
type Money = string & { __brand: "Money" };

export interface Transaction {
  amount: Money;
  postedDateTime: string;
}

let foo: DeepPartial<Transaction> = {
  postedDateTime: "2019-11-01"
};

const bar: Transaction = {
  amount: "300.50" as Money,
  postedDateTime: "2019-11-02"
};

foo = bar; // barfs with compile error

The error:

Error:(29, 1) TS2322: Type 'Transaction' is not assignable to type 'DeepPartial<Transaction>'.
  Types of property 'amount' are incompatible.
    Type 'Money' is not assignable to type 'undefined'.

What is the expected behavior?

An instance of T is assignable to a variable of type DeepPartial<T>, even if T includes properties that are branded strings / branded numbers.

Environment Details

Redux 4.1.2
TypeScript 4.5.2

Unsure as to whether it worked with previous versions of Redux.

@timdorr
Copy link
Member

timdorr commented Dec 13, 2021

This is actually already fixed and tested against in 5.0. I suggest giving the alpha a try.

@timdorr timdorr closed this as completed Dec 13, 2021
@flakey-bit
Copy link
Author

Thanks ❤️ and sorry for not checking the alpha first 🤦‍♂️

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

No branches or pull requests

2 participants