0

I'm not sure if I can't find an answer to this because it is not possible, but I find myself wanting to instantiate a class with an object like so:

class Foo {    
  constructor(
    public id: number,
    public bar: string,
    public y: number,
    public z: string
  ) {}    
}

let x = new Foo({bar: 'testing', z: 'test'})

Instead, this will attempt to set id equal to {bar: 'testing', z: 'test'} which is not what I want. I'd like it to create an instance of Foo with Foo.bar = 'testing' and Foo.z = 'test'.

Is the only way to define the object like new Foo(null, 'testing', null, 'test')?

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.