迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 TypeScript 中扩展不包含属性的接口

作者:迹忆客 最近更新:2023/01/29 浏览次数:

使用 Omit 实用程序类型来扩展不包含属性的接口,例如 type WithoutTasks = Omit<Employee, 'tasks'>;Omit 实用程序类型通过从提供的类型中选取属性并删除指定的键来构造一个新类型。

interface Employee {
  id: number;
  name: string;
  salary: number;
  tasks: string[];
}

// ✅ 1. Exclude 1 property
// 👇️ type WithoutTasks = {
//     id: number;
//     name: string;
//     salary: number;
// }
type WithoutTasks = Omit<Employee, 'tasks'>;

// --------------------------------------------------------

// ✅ 2. Exclude multiple properties
// 👇️ type WithoutIdAndTasks = {
//     name: string;
//     salary: number;
// }
type WithoutIdAndTasks = Omit<Employee, 'id' | 'tasks'>;

// --------------------------------------------------------

// ✅ 3. Exclude property and then add more properties
interface WithAddedProps extends Omit<Employee, 'tasks'> {
  country: string;
}

const example3: WithAddedProps = {
  id: 1,
  name: 'Tom',
  country: 'Germany',
  salary: 100,
};

我们使用 Omit 实用程序类型根据提供的类型构造一个新类型,并删除了指定的键。

interface Employee {
  id: number;
  name: string;
  salary: number;
  tasks: string[];
}

type WithoutTasks = Omit<Employee, 'tasks'>;

const example1: WithoutTasks = {
  id: 1,
  name: 'Alice',
  salary: 100,
};

第一个示例创建了一个新类型,它具有 Employee 类型中的所有属性,不包括 tasks 属性。

如果需要排除多个属性,可以将字符串文字的并集传递给 Omit 实用程序类型。

interface Employee {
  id: number;
  name: string;
  salary: number;
  tasks: string[];
}

type WithoutIdAndTasks = Omit<Employee, 'id' | 'tasks'>;

const example2: WithoutIdAndTasks = {
  name: 'Bob',
  salary: 100,
};

传递字符串文字的并集时,请确保使用竖线 | 分隔要排除的属性名称。 而不是逗号或任何其他分隔符。

如果我们想排除一些属性并添加更多属性,也可以使用这种方法。

interface Employee {
  id: number;
  name: string;
  salary: number;
  tasks: string[];
}

// ✅ If you need to exclude some and then add more properties
interface WithAddedProps extends Omit<Employee, 'tasks'> {
  country: string;
}

const example3: WithAddedProps = {
  id: 1,
  name: 'Tom',
  country: 'Germany',
  salary: 100,
};

country 属性仅存在于 WithAddedProps 类型中,而 tasks 属性仅存在于 Employee 类型中。

如果我们需要更改接口上特定属性的类型,可以使用相同的方法。

interface Employee {
  id: number;
  name: string;
  salary: number;
  tasks: string[];
}

interface WithAddedProps extends Omit<Employee, 'tasks'> {
  tasks: number[]; // 👈️ change type
}

const example3: WithAddedProps = {
  id: 1,
  name: 'Tom',
  salary: 100,
  tasks: [1, 2, 3],
};

我们在扩展接口时排除了 tasks 属性,然后将其类型更改为 number[]

如果直接从 Employee 接口扩展,这是不可能的,因为类型 number[] 不能分配给类型 string[]

相关文章

在 TypeScript 中声明一个 ES6 Map

发布时间:2023/03/05 浏览次数:181 分类:WEB前端

本篇文章将指导我们使用编码示例在 TypeScript 中定义 ES6 Map。 这解释了什么是 ES6 Map及其用途。 让我们首先看看 ES6 是什么以及为什么使用它们。 ES6 Map 在 ES6 之前,我们使用对象通过将

如何在 TypeScript 中合并对象

发布时间:2023/02/20 浏览次数:217 分类:编程语言

使用扩展语法 ... 合并 TypeScript 中的对象,例如 const obj3 = { ...obj1, ...obj2} 。 最终对象的类型将被成功推断,因此尝试添加或删除属性会导致类型检查器显示错误。 const obj1 = { name : 迹忆

如何在 TypeScript 中对数组进行排序

发布时间:2023/02/15 浏览次数:196 分类:编程语言

在 TypeScript 中对 Numbers 数组进行排序 使用 sort() 方法对 TypeScript 中的数组进行排序,例如 numArray.sort((a, b) = a - b) 。 sort 方法采用定义排序顺序的函数作为参数。 在对数值数组进行排序

如何在 TypeScript 中扩展类型

发布时间:2023/02/15 浏览次数:230 分类:编程语言

在 TypeScript 中扩展类型 使用交集类型来扩展 TypeScript 中的类型,例如 type TypeB = TypeA {age: number;} 。交集类型使用 符号定义,用于组合现有对象类型。 我们可以根据需要多次使用 运算符

TypeScript 中如何创建 Date 日期对象

发布时间:2023/02/14 浏览次数:219 分类:编程语言

使用 Date 类型在 TypeScript 中键入 Date 对象,例如 const date: Date = new Date() 。 Date() 构造函数返回一个类型为 Date 的对象。 该接口定义了 Date 对象上所有内置方法的类型。 // ?️ const da

在 TS 中使用 {[key:string]: string} 和 {[key:string]: any}

发布时间:2023/02/14 浏览次数:125 分类:编程语言

TypeScript 中的 {[key: string]: string} 类型 {[key: string]: string} 语法是 TypeScript 中的索引签名,当我们事先不知道类型属性的所有名称但知道值的形状时使用。 索引签名指定字符串类型的键和

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便