Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 即使参数类型正确,Typescript编译错误_Reactjs_Typescript_Redux_React Redux - Fatal编程技术网

Reactjs 即使参数类型正确,Typescript编译错误

Reactjs 即使参数类型正确,Typescript编译错误,reactjs,typescript,redux,react-redux,Reactjs,Typescript,Redux,React Redux,Typescript引发以下编译错误: Argument of type '(state: string, action: Action<string>) => string' is not assignable to parameter of type 'Reducer<string, Action<string>>'. Types of parameters 'state' and 'state' are incompatible. Ty

Typescript引发以下编译错误:

Argument of type '(state: string, action: Action<string>) => string' is not assignable to parameter of type 'Reducer<string, Action<string>>'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.  TS2345
另外,我知道通常使用
组合减速机
,并且
状态
不应该是
字符串
,但我问这个问题纯粹是出于好奇(作为typescript的初学者),因为我知道有明显的解决方法

我还尝试使用
string[]
作为状态类型,但是抛出了相同的错误,只是此时它是
string[]|未定义的

编辑3:


我意识到如果我在状态中添加一个默认值,例如
函数todos(state:string=“”,action:action)
,错误就会消失。但这是为什么呢?如果state已经是必需的、不可为null的参数,那么它不是多余的吗?在Swift和Kotlin中,只有使用nil state参数调用
todos
函数时才会抛出错误,但不需要在参数定义中提供默认值。那么为什么typescript中会出现这种情况呢?这是故意的吗?

回答编辑3中提到的确切问题,使函数参数成为默认值(就像您为
状态指定默认值那样)将此类参数的类型更改为
字符串|未定义的
(在您的情况下)。这也等于参数后面的问号。所以下面三个函数的调用签名是相同的

function todos(state: string | undefined, action: Action<string>)
function todos(state?: string, action: Action<string>)  // This example is not completly correct as you should make action optional too, by addint ? to it name
function todos(state: string = "", action: Action<string>)
函数todos(状态:字符串|未定义,操作:操作)
函数todos(state?:string,action:action)//此示例不完全正确,因为您应该通过addint将操作设置为可选的?名副其实
函数todos(状态:string=“”,操作:操作)
我从Typescript文档中建议这样做


本主题开头的错误指出,
Type'string | undefined'不可分配给Type'string'。
因此,将默认值添加到
state
会使其成为
string | undefined
类型,这使编译器感到满意。

看起来您并没有在此处共享所有相关代码。。。错误出现在某些函数中,该函数将
todos
作为某个参数,但此处显示的只是
todos
的签名,而不是该函数的签名。理想情况下,您应该提供一个,最好是一个可以链接到像这样的在线IDE的平台。你越容易让别人重复你的错误并修改你的代码,你就越有可能得到满意的答案。祝你好运嗨@jcalz,对不起,我忘了添加一些信息。我只是编辑了问题并添加了更多细节。谢谢,看起来不错。我实际上测试了它(仅第二种情况,我没有检查第一种情况,因为我认为它们只是等效的),但我被拒绝了,因为第二种情况产生了另一个错误(即TS1016)。我认为这种不一致性应该得到纠正,因为它具有误导性。
import React from 'react';
import ReactDOM from 'react-dom';
import './styles/index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {Action, createStore} from 'redux'
import {Provider} from "react-redux";

function todos(state: string, action: Action<string>) {
    return state
}

let store = createStore(todos); // PROBLEM HERE
let app = (
    <Provider store={store}>
        <App/>
    </Provider>
)

ReactDOM.render(app, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:
serviceWorker.unregister();
function todos(state: string | undefined, action: Action<string>)
function todos(state?: string, action: Action<string>)  // This example is not completly correct as you should make action optional too, by addint ? to it name
function todos(state: string = "", action: Action<string>)