[英]Typescript error :: Type 'number' is not assignable to type 'never'
I got a problems about我有一个问题
'Type 'number' is not assignable to type 'never'.'
'类型'数字'不可分配给类型'从不'。'
'Type 'number' is not assignable to type 'never'.''类型'数字'不可分配给类型'从不'。'
'Type 'string' is not assignable to type 'never'.''类型'字符串'不能分配给类型'从不'。'
'Type 'number' is not assignable to type 'never'.''类型'数字'不可分配给类型'从不'。'
on each id
, col
, color
, height
.在每个
id
、 col
、 color
、 height
上。 for 4 times which are located in ColsContextProvider
. 4 次,位于
ColsContextProvider
中。
And I think it might be problem of Colstate[]
but I cannot find the way to solve it.而且我认为这可能是
Colstate[]
的问题,但我找不到解决方法。
import React, { createContext, Dispatch, useReducer, useContext } from "react";
export const ADD_SQUARE = "ADD_SQUARE" as const;
export const CHANGE_SQUARE = "CHANGE_SQUARE" as const;
export const DELETE_SQUARE = "DELETE_SQUARE" as const;
export type Square = {
id: number;
col: number;
color: string;
height: number;
};
type ColState = Square[];
const ColsStateContext = createContext<ColState[] | void>(undefined);
type Action =
| {
type: "ADD_SQUARE";
id: number;
col: number;
color: string;
height: number;
}
| { type: "CHANGE_SQUARE"; id: number; col: number; color: string }
| { type: "DELETE_SQUARE"; id: number; col: number };
type ColsDispatch = Dispatch<Action>;
const ColsDispatchContext = createContext<ColsDispatch | undefined>(undefined);
function colsReducer(state: ColState[], action: Action) {
switch (action.type) {
case ADD_SQUARE:
return console.log("DELETE_SQUARE");
case CHANGE_SQUARE:
return console.log("CHANGE_SQUARE");
case DELETE_SQUARE:
return console.log("DELETE_SQUARE");
default:
return state;
}
}
export function ColsContextProvider({
children
}: {
children: React.ReactNode;
}) {
const [cols, dispatch] = useReducer(colsReducer, [
[
{
id: 1,
col: 1,
color: "#111",
height: 80
},
{
id: 2,
col: 1,
color: "#aca",
height: 110
}
],
[
{
id: 1,
col: 2,
color: "#cbb",
height: 35
}
],
[
{
id: 1,
col: 3,
color: "#aac",
height: 20
}
]
]);
return (
<ColsDispatchContext.Provider value={dispatch}>
<ColsStateContext.Provider value={cols}>
{children}
</ColsStateContext.Provider>
</ColsDispatchContext.Provider>
);
}
export function useColsState() {
const state = useContext(ColsStateContext);
if (!state) throw new Error("Cols provider problem");
return state;
}
export function useColsDispatch() {
const dispatch = useContext(ColsDispatchContext);
if (!dispatch) throw new Error("Cols provider problem");
return dispatch;
}
You need to return a proper state in colsReducer
.您需要在 colsReducer 中返回正确的
colsReducer
。
You're returning console.log()
in colsReducer
function.您将在
colsReducer
function 中返回console.log()
。 That makes the return type of colsReducer
void | Square[][]
这使得
colsReducer
的返回类型为void | Square[][]
void | Square[][]
, which is unable to infer, so the type of second parameter in useReducer
becomes never
. void | Square[][]
,无法推断,所以useReducer
中第二个参数的类型变成了never
。
Edit code like below, and see what you have to do.编辑如下代码,看看你必须做什么。
function colsReducer(state: ColState[], action: Action) {
// ...
}
change to:改成:
function colsReducer(state: ColState[], action: Action): ColState[] {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.