Javascript 使用接口上的Typescript错误响应Redux配置
我正在尝试学习如何用React Redux设置Typescript,以便将来我可以用TS做任何事情,只是为了学习。 我只想有一个动作,它将生成一个二维数字数组。 这就是我所做的: 我的行动Javascript 使用接口上的Typescript错误响应Redux配置,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正在尝试学习如何用React Redux设置Typescript,以便将来我可以用TS做任何事情,只是为了学习。 我只想有一个动作,它将生成一个二维数字数组。 这就是我所做的: 我的行动 import { GENERATE_PINS, AppActions } from '../types/actions'; import { generateAllPins } from '../utils/generatePins'; import { AppState } from '../configS
import { GENERATE_PINS, AppActions } from '../types/actions';
import { generateAllPins } from '../utils/generatePins';
import { AppState } from '../configStore';
import { PinArr } from '../types/pinType';
import { Dispatch } from 'react';
// TypeScript infers that this function is returning SendMessageAction
export const getPin = (pins: PinArr): AppActions => ({
type: GENERATE_PINS,
pins
});
export const startGeneratePin = () => {
return (dispatch: Dispatch<AppActions>, getState: () => AppState) => {
const pins = generateAllPins();
return dispatch(
getPin({
pins
})
);
};
};
我的类型
import { PinArr } from './pinType';
export const GENERATE_PINS = 'GENERATE_PINS';
interface GeneratePin {
type: typeof GENERATE_PINS;
pin: PinArr;
}
export type PinActionTypes = GeneratePin;
export type AppActions = PinActionTypes;
和我的数据接口
export interface PinArr {
pins: Array<Array<number>>;
}
论减速器
Type 'PinArr' is not an array type.
有人能帮我吗?我觉得,一旦我理解并解决了这种接口类型(这就是为什么我使用TS,作为一个开发人员整体发展),我就会觉得使用TS和React是合适的。
请?因为你的命名习惯(名字很重要),你把自己弄糊涂了!您声明为
PinsArray
的实际上是PinReducer
处理的状态片。我认为如果你这样命名的话,你的代码会更有意义:
type PinsArray=number[]
接口PinState{
pins:PinsArray
}
GeneratePin
指的是pin
,但您的对象提供的是pins
pin
似乎是PinArr
ok的一个属性,这是我的b。我仍然得到类型“PinArr”不是数组类型。因为PinArr
应该是一个属性为pins
的对象。如果希望PinArr
成为数组类型,则应执行type PinArr=array代码>
Type '{ type: "GENERATE_PINS"; pins: PinArr; }' is not assignable to type 'GeneratePin'.
Object literal may only specify known properties, but 'pins' does not exist in type 'GeneratePin'. Did you mean to write 'pin'?
Type 'PinArr' is not an array type.