How do I solve this property declaration error that typescript keeps complaining to me?
up vote
0
down vote
favorite
I have a piece of code in my componentDidMount lifecycle function that does the following
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
onAuthStateChanged
returns an unsubscriber function that needs to be called when the component unmounts. The problem is that if I declare the unsubscriber variable like so
constructor(props: {}) {
super(props);
this.unsubscriber: Function = null
}
typescript complains by saying that the property "unsubscriber" does not exist(also that I cannot assign to function because it is a constant or read only property). I tried doing other stuff like passing it as a state like so.
type AppState = {
user: RNFirebase.User | null;
unsubscriber: Function | null;
}
class App extends Component<{}, AppState> {
....
}
but that didn't do me any good; got the same error when I try to assign the return value from onAuthStateChanged
. this.unsubscriber = null
would work just fine if I was just doing react without typescript but I'm trying to use both.
The closest I got was this
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function;
....
}
But the error I got for this one is that it not initialized there or in the constructor and I can't assign null to it. So what can I do?
Here is the entire code that I'm working with.
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import { auth, RNFirebase } from 'react-native-firebase';
import { Login } from './screens';
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function; // This has to be initialized.
constructor(props: {}) {
super(props);
this.state = { user: null };
}
componentDidMount() {
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
}
componentWillUnmount() {
if (this.unsubscriber) {
this.unsubscriber();
}
}
render() {
const { user } = this.state;
if (!user) {
return <Login />;
}
return (
<View>
<Text>Welcome to my awesome app {user.email}!</Text>
</View>
);
}
}
export default App;
typescript react-native react-native-firebase
add a comment |
up vote
0
down vote
favorite
I have a piece of code in my componentDidMount lifecycle function that does the following
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
onAuthStateChanged
returns an unsubscriber function that needs to be called when the component unmounts. The problem is that if I declare the unsubscriber variable like so
constructor(props: {}) {
super(props);
this.unsubscriber: Function = null
}
typescript complains by saying that the property "unsubscriber" does not exist(also that I cannot assign to function because it is a constant or read only property). I tried doing other stuff like passing it as a state like so.
type AppState = {
user: RNFirebase.User | null;
unsubscriber: Function | null;
}
class App extends Component<{}, AppState> {
....
}
but that didn't do me any good; got the same error when I try to assign the return value from onAuthStateChanged
. this.unsubscriber = null
would work just fine if I was just doing react without typescript but I'm trying to use both.
The closest I got was this
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function;
....
}
But the error I got for this one is that it not initialized there or in the constructor and I can't assign null to it. So what can I do?
Here is the entire code that I'm working with.
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import { auth, RNFirebase } from 'react-native-firebase';
import { Login } from './screens';
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function; // This has to be initialized.
constructor(props: {}) {
super(props);
this.state = { user: null };
}
componentDidMount() {
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
}
componentWillUnmount() {
if (this.unsubscriber) {
this.unsubscriber();
}
}
render() {
const { user } = this.state;
if (!user) {
return <Login />;
}
return (
<View>
<Text>Welcome to my awesome app {user.email}!</Text>
</View>
);
}
}
export default App;
typescript react-native react-native-firebase
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a piece of code in my componentDidMount lifecycle function that does the following
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
onAuthStateChanged
returns an unsubscriber function that needs to be called when the component unmounts. The problem is that if I declare the unsubscriber variable like so
constructor(props: {}) {
super(props);
this.unsubscriber: Function = null
}
typescript complains by saying that the property "unsubscriber" does not exist(also that I cannot assign to function because it is a constant or read only property). I tried doing other stuff like passing it as a state like so.
type AppState = {
user: RNFirebase.User | null;
unsubscriber: Function | null;
}
class App extends Component<{}, AppState> {
....
}
but that didn't do me any good; got the same error when I try to assign the return value from onAuthStateChanged
. this.unsubscriber = null
would work just fine if I was just doing react without typescript but I'm trying to use both.
The closest I got was this
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function;
....
}
But the error I got for this one is that it not initialized there or in the constructor and I can't assign null to it. So what can I do?
Here is the entire code that I'm working with.
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import { auth, RNFirebase } from 'react-native-firebase';
import { Login } from './screens';
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function; // This has to be initialized.
constructor(props: {}) {
super(props);
this.state = { user: null };
}
componentDidMount() {
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
}
componentWillUnmount() {
if (this.unsubscriber) {
this.unsubscriber();
}
}
render() {
const { user } = this.state;
if (!user) {
return <Login />;
}
return (
<View>
<Text>Welcome to my awesome app {user.email}!</Text>
</View>
);
}
}
export default App;
typescript react-native react-native-firebase
I have a piece of code in my componentDidMount lifecycle function that does the following
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
onAuthStateChanged
returns an unsubscriber function that needs to be called when the component unmounts. The problem is that if I declare the unsubscriber variable like so
constructor(props: {}) {
super(props);
this.unsubscriber: Function = null
}
typescript complains by saying that the property "unsubscriber" does not exist(also that I cannot assign to function because it is a constant or read only property). I tried doing other stuff like passing it as a state like so.
type AppState = {
user: RNFirebase.User | null;
unsubscriber: Function | null;
}
class App extends Component<{}, AppState> {
....
}
but that didn't do me any good; got the same error when I try to assign the return value from onAuthStateChanged
. this.unsubscriber = null
would work just fine if I was just doing react without typescript but I'm trying to use both.
The closest I got was this
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function;
....
}
But the error I got for this one is that it not initialized there or in the constructor and I can't assign null to it. So what can I do?
Here is the entire code that I'm working with.
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import { auth, RNFirebase } from 'react-native-firebase';
import { Login } from './screens';
type AppState = {
user: RNFirebase.User | null;
};
class App extends Component<{}, AppState> {
private unsubscriber: Function; // This has to be initialized.
constructor(props: {}) {
super(props);
this.state = { user: null };
}
componentDidMount() {
this.unsubscriber = auth().onAuthStateChanged((user: RNFirebase.User) => {
this.setState({ user });
});
}
componentWillUnmount() {
if (this.unsubscriber) {
this.unsubscriber();
}
}
render() {
const { user } = this.state;
if (!user) {
return <Login />;
}
return (
<View>
<Text>Welcome to my awesome app {user.email}!</Text>
</View>
);
}
}
export default App;
typescript react-native react-native-firebase
typescript react-native react-native-firebase
edited Nov 11 at 3:56
asked Nov 11 at 3:44
Luis Averhoff
7419
7419
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I'd suggest you to keep your unsubscriber
declaration as a class member but make it optional (optional class properties). Also, Function
type is generally not useful at all (just take a look at what is the interface that it defines) and you're better off defining its type as () => void
if its return value is going to be ignored (see callback types). So, try something like this:
private unsubscriber?: () => void;
add a comment |
up vote
0
down vote
You just need to initialize unsubscriber
property:
private unsubscriber: (() => void) | null = null;
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I'd suggest you to keep your unsubscriber
declaration as a class member but make it optional (optional class properties). Also, Function
type is generally not useful at all (just take a look at what is the interface that it defines) and you're better off defining its type as () => void
if its return value is going to be ignored (see callback types). So, try something like this:
private unsubscriber?: () => void;
add a comment |
up vote
1
down vote
accepted
I'd suggest you to keep your unsubscriber
declaration as a class member but make it optional (optional class properties). Also, Function
type is generally not useful at all (just take a look at what is the interface that it defines) and you're better off defining its type as () => void
if its return value is going to be ignored (see callback types). So, try something like this:
private unsubscriber?: () => void;
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I'd suggest you to keep your unsubscriber
declaration as a class member but make it optional (optional class properties). Also, Function
type is generally not useful at all (just take a look at what is the interface that it defines) and you're better off defining its type as () => void
if its return value is going to be ignored (see callback types). So, try something like this:
private unsubscriber?: () => void;
I'd suggest you to keep your unsubscriber
declaration as a class member but make it optional (optional class properties). Also, Function
type is generally not useful at all (just take a look at what is the interface that it defines) and you're better off defining its type as () => void
if its return value is going to be ignored (see callback types). So, try something like this:
private unsubscriber?: () => void;
answered Nov 11 at 4:29
shkaper
740313
740313
add a comment |
add a comment |
up vote
0
down vote
You just need to initialize unsubscriber
property:
private unsubscriber: (() => void) | null = null;
add a comment |
up vote
0
down vote
You just need to initialize unsubscriber
property:
private unsubscriber: (() => void) | null = null;
add a comment |
up vote
0
down vote
up vote
0
down vote
You just need to initialize unsubscriber
property:
private unsubscriber: (() => void) | null = null;
You just need to initialize unsubscriber
property:
private unsubscriber: (() => void) | null = null;
answered Nov 11 at 4:39
Diego López
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245658%2fhow-do-i-solve-this-property-declaration-error-that-typescript-keeps-complaining%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown