[英]Type 'number' is not assignable to type 'never'
I am creating custom hook useArray in react with Typescript, I am using Generics for array type as i want any possible value as array element,but then when i try to push any number then it gives me error: Type 'number' is not assignable to type 'never'.我正在创建自定义钩子 useArray 以与 Typescript 做出反应,我将 Generics 用作数组类型,因为我希望任何可能的值作为数组元素,但是当我尝试推送任何数字时,它会给我错误:类型“数字”不可分配输入“从不”。 and same for any other type as well.if i initialise my array with some values then only that values can be pushed again and not any other values.How to solve this issue?对于任何其他类型也是如此。如果我用一些值初始化我的数组,那么只能再次推送该值而不是任何其他值。如何解决这个问题?
Here i am providing code: useArray.ts:我在这里提供代码:useArray.ts:
import { useState } from "react";
type CompType = string | number;
export default function useArray<T extends CompType>(defaultValue: T[]): {
array: T[],
set: React.Dispatch<SetStateAction<T[]>>,
push: (elemet: T) => void,
remove: (index: number) => void,
filter: (callback: (n: T) => boolean) => void,
update: (index: number, newElement: T) => void,
clear: () => void
} {
const [array, setArray] = useState(defaultValue);
function push(element: T) {
setArray((a) => [...a, element]);
}
function filter(callback: (n: T) => boolean) {
setArray((a) => a.filter(callback));
}
function update(index: number, newElement: T) {
setArray((a) => [
...a.slice(0, index),
newElement,
...a.slice(index + 1, a.length),
]);
}
function remove(index: number) {
setArray((a) => [...a.slice(0, index), ...a.slice(index + 1, a.length)]);
}
function clear() {
setArray([]);
}
return { array, set: setArray, push, filter, update, remove, clear };
ArrayComp.tsx: ArrayComp.tsx:
import useArray from "./useArray";
function ArrayComp() {
const { array, set, push, remove, filter, update, clear } = useArray([]);
return (
<div>
<h1>Updated array: </h1>
<div> {array.join(", ")} </div>
<button onClick={() => set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])}>
set to[1, 2, 3, 4, 5, 6, 7, 8, 9,10]
</button>
<button onClick={() => push(5)}> Add 5 </button>
<button onClick={() => push("Hello")}> Add "Hello" </button>
<button onClick={() => update(3, 5)}> update 5 at index 3 </button>
<button onClick={() => filter((n) => n < 10)}>
filter numbers less than 10
</button>
<button onClick={() => remove(3)}> Remove forth Element </button>
<button onClick={clear}> Empty array </button>
</div>
);
}
export default ArrayComp;
App.tsx:应用程序.tsx:
import "./App.css";
import ArrayComp from "./components/ArrayComp";
function App() {
return (
<div className="App">
<h1>React Custom Hook useArray()</h1>
<ArrayComp />
</div>
);
}
export default App;
When calling useArray()
you passed an empty array []
.调用useArray()
时,您传递了一个空数组[]
。 React doesn't really know which type the elements in the array should have based on the empty array, so he assigns T
the type unknown
.根据空数组,React 并不知道数组中的元素应该是哪种类型,因此他将T
分配为unknown
类型。
One possible solution would be to specify the correct type when calling useArray()
.一种可能的解决方案是在调用useArray()
时指定正确的类型。
const {
array, set, push, remove, filter, update, clear
} = useArray<string | number>([])
Another way to solve this is to make the defaultValue
optional.解决这个问题的另一种方法是使defaultValue
可选。
function useArray<T extends CompType>(defaultValue?: T[]): /* ... */ {
if (!defaultValue || defaultValue.length === 0) {
defaultValue = []
}
/* ... */
}
If you use useArray()
now without any args typescript will assign CompType
to T
.如果您现在使用没有任何参数的useArray()
typescript 会将CompType
分配给T
。
const { array, set, push, remove, filter, update, clear } = useArray()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.