9

I'm trying to use the updeep library. A typical example of using updeep is something like this:

var person = {
  name: {
    first: 'Jane',
    last: 'West'
  }
};

var result = u({ name: { first: 'Susan' } }, person);

The idea here is that result will be a clone of person but with the value of name.first changed. You can imagine this function, u, defined in TypeScript as:

function u<T>(changes: {}, obj: T): T { ... }

This captures the fact that the type of the second argument is also the return type of the function. But what it does not express is that changes should be a super type of T.

What I'd like is some type checking on the first argument. The point is that the values present in the changes arguement should all be present in the T type parameter and match the types of their counterparts there. Expressing this allows us to check the changes argument to make sure it makes sense with respect to the type T (i.e., is a super type of T).

I'm not sure if this is possible in TypeScript. In languages like Java, you have the super keyword and it can be used to describe constraints on type parameters.

While TypeScript doesn't allow it, something like this expresses what I would like:

function u<T extends U,U extends {}>(changes: U, obj: T): T { ... }

Does anybody have an suggestions on how to express this? It would be great to have a typesafe system for performing such transformations.

Thanks.

2
  • 1
    Did you find a solution to this? Indeed, as you stated in another comment, using Partial to achieve this makes every property optional, so type safety is compromised.
    – Laurens
    Commented May 11, 2019 at 10:07
  • @Laurens That shouldn't compromise type safety. In fact, that's exactly what's desired: an object type that's the same as T except every key is optional is a supertype of T: any T (which has all the keys) is assignable to the partial type (where the keys are optional). It's not quite every supertype, since each key's type has to be either the same as it was in T or not present at all.
    – Peeja
    Commented Aug 7, 2023 at 15:52

2 Answers 2

7

This has been possible since TypeScript 2.1 with Partial<T>

The original problem would be solved with

function u<T>(changes: Partial<T>, obj: T): T { ... }

1

There's an open issue for something like this, called "Partial types" here: https://github.com/Microsoft/TypeScript/issues/4889

Facebook's Flow has a similar (but intentionally undocumented) $Shape<> type, which can be seen in the typings for React's setState().

So basically, no, there's no such feature that can do this automatically. However, you can kind of work around it by doing this manually:

interface IPartialPerson {
  name?: {
    first?: string;
    last?: string;
  };  
  someOptionalProperty?: string;
}

interface IPerson extends IPartialPerson {
  name: {
    first: string;
    last: string;
  };
}

Personally, I prefer to even avoid the latter part and just use optional properties as much as possible. Mandatory properties don't really protect you from anything; they're not non-nullable even though they look like it.

4
  • 2
    While it is true that these are non-nullable, you are still giving up a degree of type safety here. For example, when I create a component in React, I specify a props type. Whenever I instantiate that type in TSX, anything that is not optional is, therefore, required. This requirement is enforced by the compiler. Making everything optional really opens to door for all kinds of "half-baked" instances that could create lots of mayhem. True, they can be null, but I'd have to make them null explicitly. Commented Nov 15, 2015 at 13:48
  • @MichaelTiller I do think they're useful at times, and I do tend to use non-optional properties in React props (along with an isRequired setting in propTypes!). But I worry because they appear to add more type safety than they really do, so avoid allowing myself (and therefore anybody reading the code) to assume that optional properties are protected against null values.
    – DallonF
    Commented Nov 16, 2015 at 0:08
  • 1
    If you're using Partial<> - doesn't that allow extra, incorrect properties? It doesn't actually constrain to a supertype, it constrains to a subtype of a supertype or something like that; i.e. it's way too lax. Commented Apr 5, 2019 at 9:53
  • The non-nullability issue has been closed (for a while now ;)), adding a compiler option to prevent types from being implicitly null
    – Laurens
    Commented May 11, 2019 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.