简体   繁体   English

"反应打字稿如何正确键入 SetStateAction"

[英]react typescript how to correctly type a SetStateAction

https://codesandbox.io/s/dazzling-gates-ruxzr?file=/src/TodoAdder.tsx https://codesandbox.io/s/dazzling-gates-ruxzr?file=/src/TodoAdder.tsx

so I have a simple todo app and I am tying to type the action that update the state of the todos, I am getting the following error , the error does not happen if I change my Todo type declaration to string instead of o union.所以我有一个简单的 todo 应用程序,我想输入更新 todos 状态的操作,我收到以下错误,如果我将 Todo 类型声明更改为字符串而不是 o union,则不会发生错误。 is this happening because of the union type on my Todo interface, I notice that error disappears when i use a String for my status object proprety这是因为我的Todo界面上的联合类型而发生的吗?我注意到当我使用字符串作为我的状态对象属性时错误消失了

export type Todo ={
    name: String;
    id: Number;
    status: 'Created' | 'Completed' | 'Archived';
}

在此处输入图像描述

只需为payload<\/code>设置一个类型:

const payload: Todo = {

暂无
暂无

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

相关问题 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' &#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39; - React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>' React.Dispatch 时如何为 setState function 定义 TypeScript 类型<react.setstateaction<string> &gt; 不接受? </react.setstateaction<string> - How can I define TypeScript type for a setState function when React.Dispatch<React.SetStateAction<string>> not accepted? React js Typescript “字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '</number> - React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>' 如何使用 Typescript 正确键入 react HOC - How to correctly type react HOC with Typescript 收到错误:类型'[null,Dispatch <setstateaction<null> &gt;]' 不能分配给输入 typescript 反应</setstateaction<null> - getting error :Type '[null, Dispatch<SetStateAction<null>>]' is not assignable to type in typescript react React Typescript - 类型参数不可分配给“SetStateAction”类型的参数<user | record<string, never> >'</user> - React Typescript - Argument of type is not assignable to parameter of type 'SetStateAction<User | Record<string, never>>' Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' TypeScript 自定义 useStateIfMounted React Hook - 并非所有类型为 'T | ((值:SetStateAction<t> ) =&gt; void)' 是可调用的</t> - TypeScript Custom useStateIfMounted React Hook - Not all constituents of type 'T | ((value: SetStateAction<T>) => void)' are callable Typescript 抱怨 React useState setter `Argument of type 'Dispatch <setstateaction<never[]> &gt;' 不可分配给参数</setstateaction<never[]> - Typescript complaining about React useState setter `Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM