如何处理lodash.omitBy返回值的类型?

发布于 2025-01-12 22:34:58 字数 1473 浏览 1 评论 0原文

omitFalsyFields 泛型函数将删除值为 falsy 的键(''undefinednull 等。 .) 的一个对象。

import _, { Dictionary } from 'lodash';

export function omitFalsyFields<T extends Dictionary<any>>(params: T) {
  return _.omitBy(params, (k) => !k);
}

interface CreateUserParams {
  name?: string;
  email: string;
}

const params: CreateUserParams = omitFalsyFields({ name: '', email: '[email protected]' })

我期望它的返回值的类型是CreateUserParams类型。 params 的值为 { email: '[email  ;protected]' },因此类型是兼容的使用CreateUserParams

但是,出现 TS 类型错误:

“Dictionary”类型中缺少属性“email”,但“CreateUserParams”类型中需要属性“email”

TypeScript 游乐场

omitFalsyFields generic function will remove the key which value is falsy('', undefined, null, etc...) of an object.

import _, { Dictionary } from 'lodash';

export function omitFalsyFields<T extends Dictionary<any>>(params: T) {
  return _.omitBy(params, (k) => !k);
}

interface CreateUserParams {
  name?: string;
  email: string;
}

const params: CreateUserParams = omitFalsyFields({ name: '', email: '[email protected]' })

I expect the type of its returned value is CreateUserParams type. The value of params is { email: '[email protected]' }, so the type is compatible with CreateUserParams.

But, got TS type error:

Property 'email' is missing in type 'Dictionary' but required in type 'CreateUserParams'

TypeScript Playground

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-01-19 22:34:58

如果您想使此函数通用,您可以执行以下操作:

export function omitFalsyFields<T>(params: T): Dictionary<T> {
  return _.omitBy(params, (k: any) => !k);
}

现在您可以传递任何类型,并且它应该应用于输入和输出

const params = omitFalsyFields<CreateUserParams>({
  name: "",
  email: "[email protected]"
})

但是对此需要注意的是 omitBy 返回一个 Dictionary,即使执行 Dictionary 它也总是会强制转换为 any,因为有时很难在运行时推断类型(如果不是不可能的话),因此如果您想分配一个风俗返回类型或将通用 T 类型指定为输出,请执行以下操作:

// note that T is an alias for you generic type
export function omitFalsyFields<T>(params: T) {
  // use the "as" keyword to override the returned type from omitBy.
  // With partial you change all properties of T to optional,
 // as omitBy removes falsy properties it makes sense to apply this utility
  return _.omitBy(params, (k: any) => !k) as Partial<T>;
}

const params = omitFalsyFields...
console.log(params.name) // undefined
console.log(params.email) // [email protected]

这样做,特定类型也会传递到输出,请查看 完整示例

If you want to make this function generic, you can do the following:

export function omitFalsyFields<T>(params: T): Dictionary<T> {
  return _.omitBy(params, (k: any) => !k);
}

Now you can pass any type and it should be applied to the input and output

const params = omitFalsyFields<CreateUserParams>({
  name: "",
  email: "[email protected]"
})

But one caveat about this is that omitBy returns a Dictionary<any>, even doing Dictionary<T> it will always casts to any because it's very hard to infer types at runtime if not impossible sometimes, therefore if you want to assign a custom return type or assign the generic T type as output, do the following:

// note that T is an alias for you generic type
export function omitFalsyFields<T>(params: T) {
  // use the "as" keyword to override the returned type from omitBy.
  // With partial you change all properties of T to optional,
 // as omitBy removes falsy properties it makes sense to apply this utility
  return _.omitBy(params, (k: any) => !k) as Partial<T>;
}

const params = omitFalsyFields...
console.log(params.name) // undefined
console.log(params.email) // [email protected]

Doing so, the specific type will be passed to the output as well, take a look at the complete example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文