注入商店以响应应用程序时出现打字稿错误

时间:2020-05-21 02:01:05

标签: reactjs typescript redux react-redux

我正在尝试使用Redux使用TypeScript构建React应用程序以进行状态管理,但是我在应用程序组件上遇到此错误。Jsonfile

-app.tsx

CREATE TABLE test (col1 int, col2 text);

CREATE SEQUENCE test_col1_seq OWNED BY test.col1;

ALTER TABLE test ALTER COLUMN col1 SET DEFAULT ceil(nextval('test_col1_seq')/4::numeric);

SELECT * FROM test;
 col1 | col2 
------+------
    1 | a
    1 | b
    1 | c
    1 | d
    2 | e
    2 | f
    2 | g
    2 | h
    3 | i
(9 rows)

-Store.tsx

import React from 'react';
import { Provider } from 'react-redux';
//import { Router } from "react-router";
import store from './redux/store';
import './App.css';

function App() {
  return (
    <Provider store={store}>
    </Provider>
  );
}

export default App;

我试图将商店注入索引中,但是我也遇到了相同的错误。任何问题出自何处?

1 个答案:

答案 0 :(得分:1)

这是因为TypeScript无法推断redux存储状态的类型。

如果尚未创建类型,则需要使用接口或类型别名来定义商店的类型:

export interface RootState {
  // add required properties from your store
}

在您的app.tsx上,

import { Store as ReduxStore } from 'redux';
 // other imports 

interface AppProps {
  store: ReduxStore<RootState>;
}

const App: React.FC<AppProps> = ({ store }) => (
  <Provider store={store}>
    <App onLoad={onLoad} />
  </Provider>
);