How can I define an object type with one value if the key is in an array and another value if not

Scott Bowles

I have a function something like:

function allFinished(pendingProps: string[], allProps: Record<string, unknown>): boolean {
    return pendingProps.every((pendingProp) => allProps[pendingProp].isFinished)
}

For typescript to be happy with me, it needs to know that each allProps key which is an element of pendingProps will have a value that includes isFinished. But I also need it to be okay with keys which are not in pendingProps having a different value type.

This would be fairly simple if pendingProps was defined ahead of time and could be marked as const, but I can't figure out how to do this with with an arbitrary string array.

Tobias S.

We should make allFinished generic for this to be properly typed.

function allFinished<
  T extends Record<K, { isFinished: boolean }> & Record<string, unknown>, 
  K extends string
>(pendingProps: K[], allProps: T): boolean {
    return pendingProps.every((pendingProp) => allProps[pendingProp].isFinished)
}

The essentially expresses the constraints you mentioned in your question. All pendingProps will be stored in K while the allProps object is stored in T. T is constrained so that every property where the key is part of K must have a property isFinished. But every other string property will have an unknown type.

Let's see if this works:

allFinished(["a", "b", "c"], {
    a: { isFinished: true },
    b: { isFinished: false },
    c: {},  // Error: Property 'isFinished' is missing in type '{}' but required in type '{ isFinished: boolean; }'
    d: {}
})

Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I change one value for a Object while comparing it to another array?

How can I define a Typescript object return value for a function?

How can I check if an object contains at least one key whose value contains a substring in JavaScript?

How can I detect if array have year value and define it?

How can I add a value to an array's key from another array with the same key in PHP?

How can I set array as a key value in javascript object?

How can I get the value of a group and define it in one line in JavaScript?

How can I define type for dynamic `this` value in TypeScript?

How to replace one array key value with another array index value?

How to compare one array key value to another array index value?

How can I define an array with length and some of elements' value in Java?

How do I copy key value pair references from one map to another map of the same type?

How can I remove an object from an array if I know the value of one field of the object?

make one array the key and another array the value

How do I create an array using the order and value from one and the key of another?

How can I check if one value is A or B and another value is A or B?

I want to add one array key and value with another array values

How to copy dictionary object value from one key to another?

How can I find index by key and value in an object (but its completely object without an array)

How can I add only particular key value pairs from one object to another?

How to make the value of an object of an array as the key of an object of another array

define object type with dynamic number of key and value and also that values can have string or number value

How can i separate key/value in different array object in js?

Typescript: How do I define an interface for an object type's key value pair

How can I determine the type of another property of same object according to the property's value of the object?

How can I set one array as a value of another array in PHP?

How can I change the key value in an object when creating an array value while using the map function in React?

how can i get key and value from a object of array

How can I define a mapped type that places an constraint on the value based on the key in typescript?