简体   繁体   English

面对 Object 文字可能只指定 React Typescript 中的已知属性

[英]Faced Object literal may only specify known properties in React Typescript

I was creating project in React Typescript and decided to use Hooks + useContext + useReducer.我在 React Typescript 中创建项目并决定使用 Hooks + useContext + useReducer。 THen, I created seperate file to configure initial state and provider.然后,我创建了单独的文件来配置初始 state 和提供程序。 But I face the error where ADD_TRANSACTIONS is used.但我面临使用ADD_TRANSACTIONS的错误。 Here is the code I have now:这是我现在拥有的代码:

import * as React from "react";
import { createContext, useReducer, ReactNode } from "react";
import transactionReducer from "./transactionReducer";
const initialState = {
  transactions: [
    { id: 1, text: "Cash", amount: 10000 },
    { id: 2, text: "Food", amount: -10000 },
  ],
};

export const Context = createContext(initialState);

interface Props {
  children: ReactNode;
}

const GlobalProvider = ({ children }: Props) => {
  const [state, dispatch] = useReducer(transactionReducer, initialState);

  const ADD_TRANSACTIONS = (transaction: any) => {
    dispatch({ type: "ADD_TRANSACTIONS", payload: transaction });
  };

  return (
    <Context.Provider
      value={{
        transactions: state.transactions,
        ADD_TRANSACTIONS, Here I face the error which is defined below
      }}
    >
      {children}
    </Context.Provider>
  );
};

export default GlobalProvider;

Here is that error:这是那个错误:

'{ transactions: any; ADD_TRANSACTIONS: (transaction: any) => void; }' is not assignable to type '{ transactions: { id: number; text: string; amount: number; }[]; }'.
  Object literal may only specify known properties, and 'ADD_TRANSACTIONS' does not exist in type '{ transactions: {
id: number; text: string; amount: number; }[]; }'.

You made it so your context value (what's provided to components) is the same thing as the reducer initial state.您这样做是为了使您的上下文值(提供给组件的内容)与减速器初始 state 相同。 These are different things because ADD_TRANSACTIONS should exist in the context but not in the state.这些是不同的东西,因为ADD_TRANSACTIONS应该存在于上下文中,而不是 state 中。 Here's what I would recommend trying:这是我建议尝试的方法:

import * as React from "react";
import { createContext, useReducer, ReactNode } from "react";
import transactionReducer from "./transactionReducer";

interface Transaction {
  id: number;
  text: string;
  amount: number;
}

interface State {
  transactions: Transaction;
}

interface GlobalContextType {
  addTransaction(transaction: Transaction): void;
  state: State;
}

const initialState: State = {
  transactions: [
    { id: 1, text: "Cash", amount: 10000 },
    { id: 2, text: "Food", amount: -10000 },
  ],
};

// Create context with no default value since it will be provided in the component.
// You could try to provide a default value, but it will never be fully complete
// with working functions and everything so I don't see the point in trying.
export const Context = createContext<GlobalContextType>(undefined as any);

// If you don't have any props, get rid of this and replace React.FC<Props>
// later with just React.FC
interface Props {
}

const GlobalProvider: React.FC<Props> = ({ children }) => {
  const [state, dispatch] = useReducer(transactionReducer, initialState);

  return (
    <Context.Provider
      value={{
        state,
        addTransaction(transaction) {
          dispatch({ type: "ADD_TRANSACTIONS", payload: transaction });
        }
      }}
    >
      {children}
    </Context.Provider>
  );
};

export default GlobalProvider;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript object 文字可能只指定已知属性错误 - Typescript object literal may only specify known properties error 对象字面量只能指定已知的属性,并且“任务”在类型“SetStateAction”中不存在 - Object literal may only specify known properties, and 'task' does not exist in type 'SetStateAction 对象字面量只能指定已知的属性,并且在类型 &#39;Headers&#39; 中不存在 &#39;&#39;app-id&#39;&#39; - Object literal may only specify known properties, and ''app-id'' does not exist in type 'Headers' TS 错误 Object literal may only specify known properties, and ‘state’ does not exist in type 'Partial<path> '</path> - TS error Object literal may only specify known properties, and 'state' does not exist in type 'Partial<Path>' 带Typescript抛出TypeError的React Relay(react-relay):对象原型只能是一个Object或为null:未定义 - React Relay (react-relay) with Typescript throwing TypeError: Object prototype may only be an Object or null: undefined 没有重载的面临错误与 React Typescript 中的此调用匹配 - Faced error of No overload matches this call in React Typescript 为什么我可以将未知属性分配给 typescript 中的文字 object? - Why can I assign unknown properties to literal object in typescript? 如何在使用 Typescript 时将样式化组件添加为 object 文字的属性? - How to add styled components as properties of an object literal while using Typescript? React:如何解决'Spread types may only be created from object'? - React: How to solve 'Spread types may only be created from object'? 类型断言对象字面量打字稿 - Type assertion object literal typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM