简体   繁体   English

Typescript 错误:: 类型“数字”不可分配给类型“从不”

[英]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 .在每个idcolcolorheight上。 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.

相关问题 Typescript 错误:类型 'number' 不可分配给类型 'never' - Typescript error:Type 'number' is not assignable to type 'never' TypeScript错误:类型“…”不能分配给类型“ IntrinsicAttributes&Number” - TypeScript error : Type '…' is not assignable to type 'IntrinsicAttributes & Number' reactjs + typescript 获取错误:类型“字符串”不可分配给类型“从不” - reactjs + typescript getting error: Type 'string' is not assignable to type 'never' typescript 错误 - “布尔值”不可分配给类型“数字” - typescript error - 'boolean' is not assignable to type 'number' 打字稿:不可分配给键入错误 - Typescript: is not assignable to type error 对象数组减少打字稿错误:不可分配给“从不”类型的参数 - Array of object reduce typescript error: not assignable to parameter of type 'never' Typescript 错误:键入此不能分配给键入此 - Typescript error: type this is not assignable to type this Prisma -&gt; 类型“数字”不可分配给类型“从不” - Prisma -> Type 'number' is not assignable to type 'never' 输入&#39;字符串| number&#39; 不可分配到类型 &#39;never&#39; - Type 'string | number' is not assignable to type 'never' 打字稿错误:setInterval - 类型“计时器”不可分配给类型“数字” - Typescript Error: setInterval - Type 'Timer' is not assignable to type 'number'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM