【发布时间】:2020-01-07 21:38:16
【问题描述】:
在我构建 react redux 应用程序时收到警告消息:
"message": "Type '{ type: string; payload: Text[]; }' 不可分配给 type 'MessageAction'。\n 属性 'type' 的类型不兼容。\n Type 'string' 是不可分配给类型 '\"MESSAGES_ACTIONS_SUCCESS\"'.",
所以:
src/pages/home/modules/types.ts中的1和2有什么不同
// src/pages/home/modules/types.ts
1. got warn msg
export const MESSAGES_ACTIONS_SUCCESS = "MESSAGES_ACTIONS_SUCCESS"
export interface MessageAction {
type: typeof MESSAGES_ACTIONS_SUCCESS
payload: Text[]
}
2.no warn msg
export const MESSAGES_ACTIONS_SUCCESS = "MESSAGES_ACTIONS_SUCCESS"
export interface MessageAction {
type: string
payload: Text[]
}
// src/pages/home/modules/actions.ts
import { Dispatch } from "redux"
import { MESSAGES_ACTIONS_SUCCESS, MessageAction } from "./types"
export const loadMessageData = () => async (
dispatch: Dispatch
): Promise<MessageAction> => {
const messages: Text[] = await new Promise(resolve => {
setTimeout(() => resolve([{ text: "home ~~~~~~" }]))
})
return dispatch({
type: MESSAGES_ACTIONS_SUCCESS,
payload: messages
})
}
更多信息代码回购是 https://github.com/77xi/SSR/pull/5
【问题讨论】:
标签: typescript