question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

valueChanges() loses type information

See original GitHub issue

Using Angularfire2@5.0.0-rc.3.

Calling .valueChanges() on an AngularFireObject<T> should return an Observable<T | null>, but instead returns Observable<{} | null>.

Similarly, calling .valueChanges() on an AngularFireList<T> should return an Observable<T[]>, but instead returns Observable<{}[]>.

This results in error messages for code that should be correct. For example, the following code:

interface FooBar {
	foo: string;
	bar: number;
}
afDb.object<FooBar>("/foo-bar/1")
	.valueChanges()
	.subscribe(fooBarItem => {
		if (fooBarItem !== null) {
			console.log(`Foo: ${fooBarItem.foo}, Bar:${fooBarItem.bar}`);
		}
	});

afDb.list<FooBar>("/foo-bar")
	.valueChanges()
	.subscribe(fooBarArray => {
		fooBarArray.forEach(fooBarItem => {
			console.log(`Foo: ${fooBarItem.foo}, Bar:${fooBarItem.bar}`);
		});
	});

yields the following TypeScript errors on the console.log() statements:

Property 'foo' does not exist on type '{}'. 
Property 'bar' does not exist on type '{}'. 
Property 'foo' does not exist on type '{}'. 
Property 'bar' does not exist on type '{}'. 

I think fixing this problem is simply a matter of setting the return type explicitly for the .valueChanges() function.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
jaufgangcommented, Oct 31, 2017

i think a more elegant workaround until this gets fixed would be to explicitly cast the output of .valueChanges() to your desired type

(afDb.object<FooBar>("/foo-bar/1")
	.valueChanges() as Observable<FooBar>)
	.subscribe(fooBarItem => {
		if (fooBarItem !== null) {
			console.log(`Foo: ${fooBarItem.foo}, Bar:${fooBarItem.bar}`);
		}
	});

(afDb.list<FooBar>("/foo-bar")
	.valueChanges() as Observable<FooBar[]>)
	.subscribe(fooBarArray => {
		fooBarArray.forEach(fooBarItem => {
			console.log(`Foo: ${fooBarItem.foo}, Bar:${fooBarItem.bar}`);
		});
	});
0reactions
zaki-araincommented, Oct 31, 2017

Despite creating a complete interface that reflected my database… I had to add [propName: string]: any; to my interface to get it to stop complaining… pretty ugly stuff.

Read more comments on GitHub >

github_iconTop Results From Across the Web

valueChanges() loses type information · Issue #1299 - GitHub
I think fixing this problem is simply a matter of setting the return type explicitly for the .valueChanges() function.
Read more >
Reactive Form valueChanges observable resets form value
The problem was that the form.value[field] was referenced by the item.dataList, instead of being managed separately. Share.
Read more >
Angular Reactive Forms: Tips and Tricks | by Netanel Basal
Whenever we call a control's disable() or enable() methods, Angular triggers the valueChanges event. Yes, it may surprise you because the “value” didn't ......
Read more >
5 tips on using Angular FormControl | by Alain Chautard
5. Subscribing to form updates using the valueChanges and statusChanges observables · valueChanges — Emits all value updates as soon as they ...
Read more >
How to do Conditional Validation on valueChanges method in ...
In Reactive forms both FormControls and FormGroups has a valueChanges method. It returns an observable type, so you can subscribe to it, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found