typescript 打字稿中的“不可分配给类型为 never 的参数”错误是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52423842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "not assignable to parameter of type never" error in typescript?
提问by Lev
Code is:
代码是:
const foo = (foo: string) => {
const result = []
result.push(foo)
}
I get the following TS error:
我收到以下 TS 错误:
[ts] Argument of type 'string' is not assignable to parameter of type 'never'.
[ts] 'string' 类型的参数不能分配给 'never' 类型的参数。
What am I doing wrong? Is this a bug?
我究竟做错了什么?这是一个错误吗?
回答by Tha'er M. Al-Ajlouni
All you have to do is define your result
as a string array, like the following:
您所要做的就是将您定义result
为一个字符串数组,如下所示:
const result : string[] = [];
Without defining the array type, it by default will be never
. So when you tried to add a string to it, it was a type mismatch, and so it threw the error you saw.
没有定义数组类型,默认情况下它将是never
. 所以当你试图向它添加一个字符串时,它是一个类型不匹配,所以它抛出了你看到的错误。
回答by neomib
Another way is :
另一种方法是:
const result = [] as any;
回答by Randy Hudson
This seems to be a recent regression or some strange behavior in typescript. If you have the code:
这似乎是最近的回归或打字稿中的一些奇怪行为。如果你有代码:
const result = []
Usually it would be treated as if you wrote:
通常它会被当作你写的:
const result:any[] = []
however, if you have both noImplicitAny
FALSE, ANDstrictNullChecks
TRUE in your tsconfig, it is treated as:
但是,如果您的 tsconfig 中同时具有noImplicitAny
FALSE和strictNullChecks
TRUE,则将其视为:
const result:never[] = []
This behavior defies all logic, IMHO. Turning on null checks changes the entry types of an array?? And then turning on noImplicitAny
actually restores the use of any
without any warnings??
这种行为违反了所有逻辑,恕我直言。打开空检查会更改数组的条目类型??然后打开noImplicitAny
居然恢复使用any
没有任何警告??
When you truly have an array of any
, you shouldn't need to indicate it with extra code.
当你真的有一个 的数组时any
,你不需要用额外的代码来指示它。
回答by ImFarhad
I was having same error In ReactJSstatless function while using ReactJs Hook useState. I wanted to set state of an object array , so if I use the following way
在使用 ReactJs Hook useState时,我在ReactJSstatless 函数中 遇到了同样的错误。我想设置对象数组的状态,所以如果我使用以下方式
const [items , setItems] = useState([]);
and update the state like this:
并像这样更新状态:
const item = { id : new Date().getTime() , text : 'New Text' };
setItems([ item , ...items ]);
I was getting error:
Argument of type '{ id: number; text: any }' is not assignable to parameter of type 'never'
我收到错误:
'{ id: number; 类型的参数; 文本:任何 }' 都不能分配给类型为 'never' 的参数
but if do it like this,
但如果这样做,
const [items , setItems] = useState([{}]);
Error is gone but there is an item at 0 indexwhich don't have any data(don't want that).
错误消失了,但索引为0的项目没有任何数据(不想要)。
so the solution I found is:
所以我找到的解决方案是:
const [items , setItems] = useState([] as any);
回答by neiya
I got the same error in ReactJS function component, using ReactJS useState hook. The solution was to declare the type of useState at initialisation:
我在 ReactJS 函数组件中遇到了同样的错误,使用 ReactJS useState 钩子。解决方案是在初始化时声明 useState 的类型:
const [items , setItems] = useState<IItem[]>([]); // replace IItem[] with your own typing: string, boolean...
回答by Mickers
I was able to get past this by using the Array keyword instead of empty brackets:
我能够通过使用 Array 关键字而不是空括号来解决这个问题:
const enhancers: Array<any> = [];
Use:
用:
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension())
}
回答by domready
You need to type result
to an array of string const result: string[] = [];
.
您需要键入result
string 数组const result: string[] = [];
。
回答by Lakshan Hettiarachchi
The solution i found was
我找到的解决方案是
const [files, setFiles] = useState([] as any);
回答by Ashutosh Singh
Remove "strictNullChecks": truefrom "compilerOptions"or set it to false in the tsconfig.jsonfile of your Ng app. These errors will go away like anything and your app would compile successfully.
删除真:“strictNullChecks”从“compilerOptions”或将其设置为false在tsconfig.json您伍应用程序文件。这些错误会像任何事情一样消失,您的应用程序将成功编译。
Disclaimer: This is just a workaround. This error appears only when the null checks are not handled properly which in any case is not a good way to get things done.
免责声明:这只是一种解决方法。仅当未正确处理空检查时才会出现此错误,这在任何情况下都不是完成任务的好方法。