2

How to pass an object of immutable type to a function as an argument

interface ImmutableObject<T> {
  get<K extends keyof T>(name: K): T[K],
  set<S>(o: S): Immutable<T & S>,
  "value1": string,
}

function(values: ImmutableObject) {
//.. doo stuff
}

I'm getting an error

'ImmutableObject' requires 1 type argument(s).

2 Answers 2

2

ImmutableObject is a generic interface. The actual data of the object is determined by the T parameter. You need to specify the T argument to the immutable object

interface ImmutableObject<T> {
  get<K extends keyof T>(name: K): T[K],
  set<S>(o: S): Immutable<T & S>,
}

function foo(values: ImmutableObject<{ value1: string }>) {
  values.get('value1')
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Let your function pass through type parameter of immutable object.

function <T>(values: ImmutableObject<T>) {
//.. doo stuff
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.