Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Reactotron.log + typescript - cannot invoke an object which is possibly 'undefined' #239

Closed
doronnac opened this issue Feb 10, 2020 · 7 comments

Comments

@doronnac
Copy link

reactotron-react-native v4.0.3

import Reactotron from 'reactotron-react-native'
Reactotron.log() // <-- Cannot invoke an object which is possibly 'undefined'

Saw a similar issue which was supposedly resolved. Maybe I'm missing something?

@DavidNeiOs
Copy link

I found a temporary solution in this thread #168 (comment)
@doronnac

@leotm
Copy link

leotm commented Jul 13, 2020

Since in our ReactotronCore interface log?: (...args: any[]) => void is an optional function, we just need to assert it's not null when calling it:

Reactotron.log!('hey there')

Same goes for any other optional functions you're calling like warn, error, etc.

Use newer optional chaining (TS 3.7+) if preferred.

Bonus: Here's a way to add type safety with linting

@patrick-narciso
Copy link

The @leotm solution is correct, it even works for other cases such as when using Reactotron.createEnhancer!()

@AndreCosta101
Copy link

The @leotm solution is correct, it even works for other cases such as when using Reactotron.createEnhancer!()

Hey Patrick... you mean in the store configuration? How did you do it ? thanks

@david-bishop-levelstudio
Copy link

david-bishop-levelstudio commented May 24, 2021

Hey Patrick... you mean in the store configuration? How did you do it ? thanks

@AndreCosta101 Something like this:

import { createStore } from "redux";
import Reactotron from "../ReactotronConfig";
import rootReducer from "./reducers";

export default createStore(rootReducer, Reactotron.createEnhancer!());

@GermaVinsmoke
Copy link

But this solution gives this warning - "Forbidden non-null assertion."
Is it okay to have this warning?

@leotm
Copy link

leotm commented Feb 10, 2022

@GermaVinsmoke

Use newer optional chaining if preferred.

Reactotron.log?.('hey there')

Since TypeScript 3.7

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants