目前,我尝试开发我的第一个 React & redux 和 typescript 示例,但我被困在 Action 声明中。 typescript 编译器仍然一遍又一遍地抛出相同的错误。
也许我误解了一些概念。
代码基于this教程。
很乐意提供任何帮助
图形 Action .ts
/// <reference path="../../../typings/index.d.ts"/>
import { createAction, Action } from 'redux-actions';
import * as _ from 'lodash';
import {IGraphObject} from "../components/graph/GraphObject";
import {Graph} from "../models/Graph";
import {ADD_GRAPH} from "../constants/ActionTypes";
const addGraph = createAction<Graph>(
ADD_GRAPH,
(graph: IGraphObject) => ({graphObject: graph})
);
export {
addGraph
}
图形.ts
import {IGraphObject} from "../components/graph/GraphObject";
export type Graph = {
id?: number;
graphObject: IGraphObject
}
图形对象.ts
export interface IGraphObject {
id?: number;
graphConfig: IGraphConfig;
graphInstance: any;
initGraph(container: HTMLElement);
addShape(shape: ICustomShapeElement);
}
typescript 编译器抛出
(11,5): error TS2345: Argument of type '(graph: IGraphObject) => { graphObject: IGraphObject; }' is not assignable to parameter of type '(...args: { id?: number; graphObject: IGraphObject; }[]) => { id?: number; graphObject: IGraphObj...'.
Types of parameters 'graph' and 'args' are incompatible.
Type '{ id?: number; graphObject: IGraphObject; }' is not assignable to type 'IGraphObject'.
Property 'graphConfig' is missing in type '{ id?: number; graphObject: IGraphObject; }'.
最佳答案
解决了。查看 redux-actions 类型对我有帮助。
redux-actions 类型
export function createAction<Input, Payload>(
actionType: string,
payloadCreator?: PayloadCreator<Input, Payload>
): (...args: Input[]) => Action<Payload>;
解决方案
const addGraph = createAction<IGraphObject, Graph>(
ADD_GRAPH,
(graphObject: IGraphObject) => ({graphObject: graphObject})
);
关于javascript - redux-actions Typescript 声明一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39596194/