Corbfon
Corbfon

Reputation: 3634

Shorthand for adding type to Observable in Typescript

Some Background

Oftentimes with typescript, I'll declare an object shape in shorthand. Instead of making an interface, and then saying that the object is of that type, I just say:

object: { fizz: boolean, buzz: boolean } = { fizz: false, buzz: true }

That makes a lot of sense to me from a code-length perspective, for the case when I'm only going to use that object shape one time.

The Question

Is there a way to tell an rxjs Observable an object shape in a similar way? I'd think it would be something like:

public getBazz(): Observable<{ fizz: boolean, buzz: boolean }> { ... }

But Typescript doesn't like that particular format, and I'm tired of guessing :)

Upvotes: 1

Views: 166

Answers (1)

Daniel Cooke
Daniel Cooke

Reputation: 1634

What version of TS are you using?

The syntax you are suggesting seems to work for me on a fresh JSbin using latest Typescript and RxJS v 5.03

var r : Observable<{prop: string}> = Rx.Observable.from([
  {prop: 'hello'},
  {prop: 'world'},
]);

r.subscribe(val => console.log(val));

Upvotes: 1

Related Questions