在 TypeScript 中强制执行对象值的类型
使用 Record 实用程序类型强制执行 TypeScript 中对象值的类型,例如 type Animal = Record<string, string>
。 Record 实用程序类型构造一个对象类型,其键和值具有特定类型。
// 👇️ function returning an object
// whose keys and values are of type string
function getObj(): Record<string, string> {
return { name: 'Tom', country: 'Chile' };
}
type Animal = Record<string, string>;
const a1: Animal = { name: 'Alfred', type: 'dog' };
Record 实用程序类型可用于将一种类型的属性映射到另一种类型。
我们传递给泛型的第一个类型是键的类型,第二个是值的类型。
type Animal = Record<string, number>;
const a1: Animal = { age: 3, num: 6 };
如果我们提前知道键的名称,则可以使用 union
来限制它们。
type Animal = Record<'name' | 'type' | 'age', string | number>;
const a1: Animal = { name: 'Alfred', type: 'dog', age: 3 };
在示例中,我们将对象键入为仅具有字符串或数字类型的键
name
、type
和age
。
如果我们尝试在对象上设置具有不同名称的键,类型检查器将抛出错误。
type Animal = Record<'name' | 'type' | 'age', string | number>;
// ⛔️ Error: Type '{ country: string; type: string; age: number; }'
// is not assignable to type 'Animal'.
const a1: Animal = { country: 'UK', type: 'dog', age: 3 };
下面是一个将对象的键限制为特定字符串的并集并将其值设置为包含 role 和 salary 属性的对象的示例。
interface EmployeeData {
role: string;
salary: number;
}
type EmployeeName = 'tom' | 'alice' | 'jeff';
const employees: Record<EmployeeName, EmployeeData> = {
tom: { role: 'accountant', salary: 10 },
alice: { role: 'manager', salary: 15 },
jeff: { role: 'programmer', salary: 17 },
};
尝试添加对象上不存在的键会导致类型检查器抛出错误。
interface EmployeeData {
role: string;
salary: number;
}
type EmployeeName = 'tom' | 'alice' | 'jeff';
const employees: Record<EmployeeName, EmployeeData> = {
tom: { role: 'accountant', salary: 10 },
alice: { role: 'manager', salary: 15 },
jeff: { role: 'programmer', salary: 17 },
// ⛔️ Error: Object literal may only specify known
// properties, and 'ben' does not exist in type
// 'Record<EmployeeName, EmployeeData>'.
ben: { role: 'programmer', salary: 17 },
};
Record
实用程序类型允许我们在键入对象的键和值时尽可能宽泛或狭窄。
如果我们知道某些对象键的名称,但希望能够包含具有其他名称的键(只要它们具有正确类型的值),请使用 union
类型。
interface EmployeeData {
role: string;
salary: number;
}
type EmployeeName = 'tom' | 'alice' | 'jeff';
// 👇️ use Union here
const employees: Record<string | EmployeeName, EmployeeData> = {
tom: { role: 'accountant', salary: 10 },
alice: { role: 'manager', salary: 15 },
jeff: { role: 'programmer', salary: 17 },
ben: { role: 'programmer', salary: 17 },
};
即使
ben
不在EmployeeName
类型中,我们也可以将属性添加到对象,因为它有一个EmployeeData
类型的值。
这使我们能够具体说明我们知道的键的名称,但仍然允许使用其他键,只要它们具有正确类型的值即可。
相关文章
比较 Pandas DataFrame 对象
发布时间:2024/04/21 浏览次数:79 分类:Python
-
本教程介绍了我们如何在 Python 中比较 Pandas DataFrame 对象。比较 DataFrames 对检查 DataFrames 之间的差异非常有帮助。
从 JavaScript 中的 JSON 对象获取值
发布时间:2024/03/22 浏览次数:177 分类:JavaScript
-
通过 JSON.parse() 方法访问 JavaScript 中的 JSON 对象和数组可以有多种做法。可以使用点(.) 操作或括号对([]) 访问它。
在 JavaScript 中使用 Fetch API 发布一个 JSON 对象
发布时间:2024/03/21 浏览次数:142 分类:JavaScript
-
在本教程中,我们将讨论如何使用 JavaScript 中的 fetch 方法将 JSON 数据作为 POST 请求发送到服务器。
在 JavaScript 中计算对象中的键的数量
发布时间:2024/03/17 浏览次数:134 分类:JavaScript
-
在今天的文章中,我们将学习如何在 JavaScript 中计算对象中键的数量。
在 PowerShell 中退出 Foreach 对象
发布时间:2024/02/05 浏览次数:158 分类:编程语言
-
本教程将教你在 PowerShell 中退出 ForEach-Object ForEach-Object cmdlet 允许用户遍历集合并对输入对象集合中的每个项目进行操作。
显示 PowerShell 对象的所有属性
发布时间:2024/02/05 浏览次数:115 分类:编程语言
-
可以使用 Get-WmiObject cmdlet 检索 WMI 对象。它将仅显示每个对象的有限数量的属性。可以使用 Format-List cmdlet 来传递结果并显示所有可用的属性。