metamp_mvc
metamp_mvc

Reputation: 65

How to pass Typescript Immutable JS type as function argument

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).

Upvotes: 2

Views: 448

Answers (2)

Ebuall
Ebuall

Reputation: 1436

Let your function pass through type parameter of immutable object.

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

Upvotes: 0

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249466

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')
} 

Upvotes: 2

Related Questions