最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Type 'Firebase' is not assignable to type 'null'. TS2322 - Stack Overflow

programmeradmin0浏览0评论

I'm following along with this tutorial:

And I'm at the step where we at the Firebase Context provider with the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import App from './ponents/App';
import Firebase, { FirebaseContext } from './ponents/Firebase';

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
);
serviceWorker.unregister();

I wanted to do this in typescript just to build my chops and on the value={new Firebase()} I'm getting the error Type 'Firebase' is not assignable to type 'null'. TS2322

I'm exactly sure how to edit the value data type assignment as it appears to be set by React itself?

This is the code for the context provider:

import React from 'react';

const FirebaseContext = React.createContext(null);

export default FirebaseContext;

I'm following along with this tutorial: https://www.robinwieruch.de/plete-firebase-authentication-react-tutorial

And I'm at the step where we at the Firebase Context provider with the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import App from './ponents/App';
import Firebase, { FirebaseContext } from './ponents/Firebase';

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
);
serviceWorker.unregister();

I wanted to do this in typescript just to build my chops and on the value={new Firebase()} I'm getting the error Type 'Firebase' is not assignable to type 'null'. TS2322

I'm exactly sure how to edit the value data type assignment as it appears to be set by React itself?

This is the code for the context provider:

import React from 'react';

const FirebaseContext = React.createContext(null);

export default FirebaseContext;
Share Improve this question edited Dec 23, 2019 at 6:22 glitchwizard asked Dec 23, 2019 at 6:09 glitchwizardglitchwizard 5521 gold badge8 silver badges26 bronze badges 4
  • What value you have set in firebaseContext? – farooq Commented Dec 23, 2019 at 6:19
  • @farooq I guess I don't know enough about React/Typescript to know how to set that value? – glitchwizard Commented Dec 23, 2019 at 6:21
  • Inside src/ponents/Firebase/context.js , What is there ? Can you modify your question with code? – farooq Commented Dec 23, 2019 at 6:22
  • @farooq just added it. – glitchwizard Commented Dec 23, 2019 at 6:23
Add a ment  | 

1 Answer 1

Reset to default 14

inside your firebase context you need to set the type, you should be able to use any for now until you figure out the specific type Firebase returns.

const FirebaseContext = React.createContext<any|null>(null);
发布评论

评论列表(0)

  1. 暂无评论