Type must be an interface property

Nerdragen

I feel like I'm missing something extremely simple here - or that it's completely impossible. But given

interface ITest {
    prop1: number
    prop2: string
}

const obj: ITest = { prop1: 10, prop2: 'asd' };

const func = (newValue: {???}) => {
    obj = {...obj, ...newValue};
}

How can I make it so newValue must be an object who's key name must exist in ITest, and who's value must the the type as defined in ITest[key]. Something like:{ [key in keyof IEditorStore ]: any }but the only accepted value type is really only { prop1: number } or { prop2: string }.

CRice

One straightforward way to do this would be to use the Partial helper and have your argument be of the type Partial<ITest>. This will restrict it to be an object with only known keys but doesn't enforce that there is only a single key (or any at all). Eg,

const func = (newValue: Partial<ITest>) => {
    obj = {...obj, ...newValue};
}

func({}); // No keys is fine.
func({prop1: 1, prop2: "a"}); // Also including both is fine.

If you want to ensure that you don't pass an empty object as an argument, you can change this to require that at least one key always be present:

type KeyValueOf<T extends {}> = {[K in keyof T]: Pick<T, K>}[keyof T];

const func = (newValue: KeyValueOf<ITest>) => {
    obj = {...obj, ...newValue};
}

func({}); // This is no longer allowed, produces an error.
func({prop1: 1, prop2: "a"}); // Both keys still allowed

I don't think there is a way to prevent both keys being allowed here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declaring property of interface type inside another interface

From Dev

The target type of this expression must be a functional interface

From Dev

DLang - Template constraints - type must implement interface

From Dev

Java "target type of lambda conversion must be an interface"

From Dev

Java: type parameter must implement interface

From Dev

The target type of this expression must be a functional interface in java

From Dev

Passing in a "TYPE" to a Generic_Interface type Property

From Dev

Attribute Value must be Declared for Element Type Property

From Dev

The content of the element type "property" must match

From Dev

Actions must have a type property error NgRx

From Dev

The target type of this expression must be a functional interface -Java 8 Functoinal Interface

From Dev

Type The type cannot be a superinterface of Do; a superinterface must be an interface

From Dev

Typescript: Named property type must be assignable to string indexer type

From Java

Is there a way to "extract" the type of TypeScript interface property?

From Dev

convert interface property 'unknown' to 'type' in TypeScript

From Dev

Set property (type interface) without implementation

From Dev

How to override an interface property with an inherited type

From Dev

default property when Com interface type is IDispatch

From Dev

Why is the generated wrapper for property type selecting this interface?

From Dev

convert interface property 'unknown' to 'type' in TypeScript

From Dev

C# Interface containing a property with an enum type

From Dev

Why does AutoMapper try to map a null property if the property is of an interface type?

From Dev

The type SlidingActivity cannot be a superinterface of MainActivity; a superinterface must be an interface

From Dev

AFNetworking 2.0 ERROR:Property with 'retain (or strong)' attribute must be of object type

From Dev

iCalendar UNTIL rule must be of the same type as DTSTART property?

From Dev

AWS CloudFormation Error 'Value of property AlarmActions must be of type List of String'

From Dev

AFNetworking 2.0 ERROR:Property with 'retain (or strong)' attribute must be of object type

From Dev

Typescript interface set type of property from another property from the same interface

From Dev

Unable to extend interface in Typescript with data property on even with the same data type

Related Related

  1. 1

    Declaring property of interface type inside another interface

  2. 2

    The target type of this expression must be a functional interface

  3. 3

    DLang - Template constraints - type must implement interface

  4. 4

    Java "target type of lambda conversion must be an interface"

  5. 5

    Java: type parameter must implement interface

  6. 6

    The target type of this expression must be a functional interface in java

  7. 7

    Passing in a "TYPE" to a Generic_Interface type Property

  8. 8

    Attribute Value must be Declared for Element Type Property

  9. 9

    The content of the element type "property" must match

  10. 10

    Actions must have a type property error NgRx

  11. 11

    The target type of this expression must be a functional interface -Java 8 Functoinal Interface

  12. 12

    Type The type cannot be a superinterface of Do; a superinterface must be an interface

  13. 13

    Typescript: Named property type must be assignable to string indexer type

  14. 14

    Is there a way to "extract" the type of TypeScript interface property?

  15. 15

    convert interface property 'unknown' to 'type' in TypeScript

  16. 16

    Set property (type interface) without implementation

  17. 17

    How to override an interface property with an inherited type

  18. 18

    default property when Com interface type is IDispatch

  19. 19

    Why is the generated wrapper for property type selecting this interface?

  20. 20

    convert interface property 'unknown' to 'type' in TypeScript

  21. 21

    C# Interface containing a property with an enum type

  22. 22

    Why does AutoMapper try to map a null property if the property is of an interface type?

  23. 23

    The type SlidingActivity cannot be a superinterface of MainActivity; a superinterface must be an interface

  24. 24

    AFNetworking 2.0 ERROR:Property with 'retain (or strong)' attribute must be of object type

  25. 25

    iCalendar UNTIL rule must be of the same type as DTSTART property?

  26. 26

    AWS CloudFormation Error 'Value of property AlarmActions must be of type List of String'

  27. 27

    AFNetworking 2.0 ERROR:Property with 'retain (or strong)' attribute must be of object type

  28. 28

    Typescript interface set type of property from another property from the same interface

  29. 29

    Unable to extend interface in Typescript with data property on even with the same data type

HotTag

Archive