Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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
Javascript 为什么Typescript在键入Redux saga调用参数-TS2339时抛出错误_Javascript_Reactjs_Typescript_Redux_Redux Saga - Fatal编程技术网

Javascript 为什么Typescript在键入Redux saga调用参数-TS2339时抛出错误

Javascript 为什么Typescript在键入Redux saga调用参数-TS2339时抛出错误,javascript,reactjs,typescript,redux,redux-saga,Javascript,Reactjs,Typescript,Redux,Redux Saga,在TS中遇到TS2339键入错误,表示找不到接口类型中定义的参数。以下是我有疑问的两个文件: api-service-saga.ts 行const res=yield callfetchAllBooks,action.param上出错; 它抱怨IBookAction和ILoadSearchableBooks上不存在../actions的参数属性类型IBookAction 因为IBookAction可以是IFetchSearchableBookAction或ILoadSearchableBooks

在TS中遇到TS2339键入错误,表示找不到接口类型中定义的参数。以下是我有疑问的两个文件:

api-service-saga.ts 行const res=yield callfetchAllBooks,action.param上出错; 它抱怨IBookAction和ILoadSearchableBooks上不存在../actions的参数属性类型IBookAction 因为IBookAction可以是IFetchSearchableBookAction或ILoadSearchableBooks,而ILoadSearchableBooks没有参数属性。也就是说,只能50%确定操作有param prop,但不能100%

若操作是ILoadSearchableBooks而不是IFetchSearchableBookAction,那个么参数可能是未定义的

解决方法是键入guard,让Typescript知道此操作确实是IFetchSearchableBookAction,而不是ILoadSearchableBooks

例如:

函数SearchableBooksAgaAction:iLocadSearchableBooks | IFetchSearchableBookAction{ 如果“param”正在运行{ 常量参数=action.param; }否则{ const books=action.searchableBooks; } } 请参阅有关如何在Typescript中键入guard的更多信息

import { takeEvery, call, put } from 'redux-saga/effects';
import { Action, IBookAction } from '../actions';
import { fetchAllBooks } from '../service/books-service';
import { Book } from '../classes';

export default function* watcherSaga() {
    yield takeEvery(Action.FETCH_SEARCHABLE_BOOKS, searchableBooksSaga);
}

function* searchableBooksSaga(action: IBookAction) {
    try {
        const res = yield call(fetchAllBooks, action.param);
        const searchableBooks = res.data.reduce((books: Array<Book>, b: any) => {
            books.push(new Book(b));
            return books;
        }, [])
        yield put({ type: Action.LOAD_SEARCHABLE_BOOKS, searchableBooks });
    } catch (e) {
        yield put({ type: Action.FETCH_ERROR, error: e});
    }

}
import { Action } from './action-constants';
import { Book } from '../classes';

export interface IAction {
    type: Action
}

// TODO: refactor into other action files if it gets too large.

// Nav Actions

export interface INavigateAction extends IAction {
    payload: string
}

export function navigateAction(newState: any): INavigateAction {
    console.log('Navigating to::', newState);
    return { type: Action.NAVIGATE_TO, payload: newState }
}

// Book Actions

export interface IFetchSearchableBookAction extends IAction {
    param: any // TODO: properly type the parameters.
}
export function fetchSerachableBooks(param: any): IBookAction {
    console.log('Fetching Searchable Books:', param);
    return { type: Action.FETCH_SEARCHABLE_BOOKS, param }
}

export interface ILoadSearchableBooks extends IAction {
    searchableBooks: Array<Book>
}

export { Action } from './action-constants';
export type IBookAction = IFetchSearchableBookAction | ILoadSearchableBooks;
TS2339: Property 'param' does not exist on type 'IBookAction'.
Property 'param' does not exist on type 'IFetchSearchableBookAction'