Reputation: 1346
I'm new to typescript and im getting the error
Type '{}' is not assignable to type 'Profile'.
on this.profile how can I fix this?
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore } from 'angularfire2/firestore';
import { Profile } from './../../models/profile';
@IonicPage()
@Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class ProfilePage {
profile = {} as Profile;
constructor(private afs: AngularFirestore, private afAuth: AngularFireAuth,
public navCtrl: NavController, public navParams: NavParams) {
}
ionViewWillLoad() {
this.afAuth.authState.take(1).subscribe(data => {
let profileCollection = this.afs.collection('profiles').doc(`${data.uid}`).valueChanges();
profileCollection.subscribe(profile => this.profile = profile);
})
}
}
Upvotes: 0
Views: 967
Reputation: 3253
Your problem here is that probably your Profile
type has mandatory properties. You just have to either make them optional or initialize them to default values. You can’t assign an empty object to a type with properties.
What you can do is also just say profile: Profile
without initialising it. You’re still enforcing the type. Since you are only assigning it inside the subscribe you don’t need to do the {}
empty object assignment.
Upvotes: 1
Reputation: 276393
Type '{}' is not assignable to type 'Profile'.
If you want to use an assertion like that you need to use the double assertion pattern i.e.
profile = {} as any as Profile;
double assertion typescript
Upvotes: 0