从打字稿中的对象中删除null属性

发布于 2025-01-28 13:10:39 字数 1022 浏览 2 评论 0原文

作为参考从javascript中的对象中删除空白属性,如何使IT typescript兼容?

JS函数(嵌套对象| ES10 ):

function removeEmpty(obj) {
  return Object.fromEntries(
    Object.entries(obj)
      .filter(([_, v]) => v != null)
      .map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
  );
}

我尝试了dubl< t,null>,但它与嵌套对象无效,我认为这不是正确的使用的实用程序。

请注意,返回的类型应删除null类型,但要保留undefined

示例/预期行为:

type TestType = {
  a?: {
    b?: {
      c: string;
    } | null;
    z?: {
      x: string;
    };
  } | null;
};

const testObj: TestType = {
  a: {
    b: null,
    z: { x: 'Hi' }
  }
};

const resultObj = removeEmpty(testObj);

其中resultobj类型类似于:

type ResultTestTypeExample = {
  a?: {
    b?: {
      c: string;
    };
    z?: {
      x: string;
    };
  };
};

As reference Remove blank attributes from an Object in Javascript, how to make it Typescript compatible?

JS function (nested objects | ES10):

function removeEmpty(obj) {
  return Object.fromEntries(
    Object.entries(obj)
      .filter(([_, v]) => v != null)
      .map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
  );
}

I've tried with Exclude<T, null> but it doesn't works with nested objects and I think it's not the correct utility to use.

Note that the returned type should remove null type(s) but keeps undefined.

Example/Expected behaviour:

type TestType = {
  a?: {
    b?: {
      c: string;
    } | null;
    z?: {
      x: string;
    };
  } | null;
};

const testObj: TestType = {
  a: {
    b: null,
    z: { x: 'Hi' }
  }
};

const resultObj = removeEmpty(testObj);

Where resultObj type is similar to:

type ResultTestTypeExample = {
  a?: {
    b?: {
      c: string;
    };
    z?: {
      x: string;
    };
  };
};

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

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

发布评论

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

评论(2

薄荷梦 2025-02-04 13:10:39

这个适合您吗?

type ExpandRecursively<T> = T extends object
  ? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
  : T;

type RemoveNull<T> = ExpandRecursively<{ [K in keyof T]: Exclude<RemoveNull<T[K]>, null> }>

Usage:

function removeEmpty<T>(obj: T): RemoveNull<T> {
  return Object.fromEntries(
    Object.entries(obj)
      .filter(([_, v]) => v != null)
      .map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
  ) as RemoveNull<T>
}

Playground

Does this work for you?

type ExpandRecursively<T> = T extends object
  ? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
  : T;

type RemoveNull<T> = ExpandRecursively<{ [K in keyof T]: Exclude<RemoveNull<T[K]>, null> }>

Usage:

function removeEmpty<T>(obj: T): RemoveNull<T> {
  return Object.fromEntries(
    Object.entries(obj)
      .filter(([_, v]) => v != null)
      .map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
  ) as RemoveNull<T>
}

Playground

悍妇囚夫 2025-02-04 13:10:39
export function removeNull<T>(obj: T | any) {
  Object.keys(obj).forEach((key) => {
    if (obj[key] && typeof obj[key] === 'object') removeNull(obj[key])
    else if (obj[key] == null) delete obj[key]
  })
  return obj
}

您可以使用上面的代码删除null元素。您还可以给您的类型,它将删除所有嵌套对象属性

export function removeNull<T>(obj: T | any) {
  Object.keys(obj).forEach((key) => {
    if (obj[key] && typeof obj[key] === 'object') removeNull(obj[key])
    else if (obj[key] == null) delete obj[key]
  })
  return obj
}

You can remove null elements with the code above. You can also give your type and it will remove all nested objects properties

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