typescript 中 type is not assignable to type 'never' 错误
当我们声明一个空数组而不显式键入它并尝试改变数组时,会出现错误“type is not assignable to type 'never' ”。 要解决该错误,需要显式键入空数组,例如 const arr: string[] = [];
。
下面是产生上述错误的示例代码
// 👇️ const arr: never[]
const arr = [];
// ⛔️ error: type 'string' is not assignable to type 'never'.ts(2322)
arr[0] = 'a';
我们声明了一个空数组,它被分配了一个 never[]
类型。 此类型表示一个永远不会包含任何元素(始终为空)的数组。
为了解决这个问题,我们必须显式键入空数组。
const arr: string[] = [];
arr[0] = 'a';
console.log(arr); // 👉️ ['a']
我们将数组键入为 string[]
,换句话说,一个仅包含字符串的数组。
如果我们不知道数组包含什么类型的元素并且想禁用类型检查,我们可以将其键入为 any[]
。
const arr: any[] = [];
arr[0] = 'a';
arr[1] = 100;
arr[2] = { department: 'development' };
// 👇️ ['a', 100, {department: 'development'}]
console.log(arr);
这有效地禁用了类型检查,这意味着数组可以包含任何类型的元素。
下面是一个如何键入对象数组的示例。
const arr: { name: string; salary: number }[] = [];
arr[0] = { name: 'tom', salary: 100 };
arr[1] = { name: 'tim', salary: 200 };
// 👇️ [{name: 'tom', salary: 100}, {name: 'tim', salary: 200}]
console.log(arr);
数组中的每个对象都有 name
和 salary
属性。
我们还可以将对象的类型提取到类型别名或接口中。
type employee = {
name: string;
salary: number;
tasks: string[];
};
const arr: employee[] = [];
arr[0] = { name: 'tom', salary: 100, tasks: ['task 1'] };
arr[1] = { name: 'tim', salary: 200, tasks: ['task 2', 'task 3'] };
出现错误“argument of type is not assignable to parameter of type 'never' ”的原因是,当我们声明一个空对象时,typescript 将其类型推断为 never[]
——一个永远不会包含任何元素的数组。
// 👇️ const arr: never[]
const arr = [];
// 👇️ const employee: {
// tasks: never[];
// }
const employee = {
tasks: [],
};
但是,typescript 会根据
tsconfig.json
文件中的设置以不同方式推断存储空数组的变量类型。
当我们将 noimplicitany
设置为 false 并将 strictnullchecks
设置为 true 时,空数组的类型将被推断为 never[]
。
{
"compileroptions": {
"strictnullchecks": true,
"noimplicitany": false,
// ... rest
}
}
但是,如果我将 noimplicitany
切换为 true,存储空数组的变量类型将被推断为 any[]
。
{
"compileroptions": {
"strictnullchecks": true,
"noimplicitany": true,
// ... rest
}
}
现在 arr 变量被推断为 any[]
。
// 👇️ const arr: any[]
const arr = [];
// 👇️ const employee: {
// tasks: never[];
// }
const employee = {
tasks: [],
};
请注意
,存储空数组的变量现在具有any[]
的推断类型,换句话说,一个禁用类型检查的数组(可以包含任何类型的元素)。
但是,对象中的空数组属性仍然是never[]类型。
noimplicitany
选项会导致 typescript 在不存在值的类型注释时发出错误,因此它必须隐式地将其类型推断为 any
。
这种行为非常不直观,最好不要依赖这样的东西。
在声明对象时显式键入空对象数组始终是最佳做法。
我们可以依靠 typescript 来推断声明为内联的文字类型(例如字符串和数字),但对数组或对象这样做绝不是一个好主意。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 angularjs 中设置 select from typescript 的默认选项值
发布时间:2023/04/14 浏览次数:132 分类:angular
-
本教程提供了在 angularjs 中从 typescript 中设置 html 标记选择的默认选项的解释性ag捕鱼王app官网的解决方案。
在 angular 中使用 typescript 的 getelementbyid 替换
发布时间:2023/04/14 浏览次数:259 分类:angular
-
本教程指南提供了有关使用 typescript 在 angular 中替换 document.getelementbyid 的简要说明。这也提供了在 angular 中 getelementbyid 的最佳方法。
在 typescript 中使用 try..catch..finally 处理异常
发布时间:2023/03/19 浏览次数:385 分类:
-
本文详细介绍了如何在 typescript 中使用 try..catch..finally 进行异常处理,并附有示例。
发布时间:2023/03/19 浏览次数:254 分类:
-
本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。
发布时间:2023/03/19 浏览次数:962 分类:
-
本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。
在 typescript 中格式化日期和时间
发布时间:2023/03/19 浏览次数:269 分类:
-
本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。
在 typescript 中返回一个 promise
发布时间:2023/03/19 浏览次数:586 分类:
-
本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。
在 typescript 中定义函数回调的类型
发布时间:2023/03/19 浏览次数:1445 分类:
-
本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。