omit
从对象中排除指定的属性
原始对象
{
"name": "Alice",
"age": 25,
"email": "alice@example.com",
"password": "secret123",
"token": "abc123xyz",
"sessionId": "1234567890"
}选择要排除的属性
omit 结果
{
"name": "Alice",
"age": 25,
"email": "alice@example.com",
"sessionId": "1234567890"
}函数签名
typescript
function omit<T extends object, K extends keyof T>(
obj: T,
keys: K[]
): Omit<T, K>参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
obj | T | 是 | 源对象 |
keys | K[] | 是 | 要排除的键名数组 |
返回值
| 类型 | 说明 |
|---|---|
Omit<T, K> | 排除指定键后的新对象 |
使用示例
typescript
const user = {
name: 'Alice',
age: 25,
email: 'alice@example.com',
password: 'secret'
}
omit(user, ['password'])
// { name: 'Alice', age: 25, email: 'alice@example.com' }