【问题标题】:Check if an object's properties are null in Typescript检查 Typescript 中对象的属性是否为空
【发布时间】:2019-01-25 04:09:21
【问题描述】:

在 Typescript 中,我正在检查对象的属性是否为空,如下所示:

var addressCountryName = response.address == null 
   ? null 
   : response.address.country == null ? null : response.address.country.name;

地址和国家都可以为空……我也试过了:

var addressCountryName = response.address?.country?.name;

这似乎不起作用...

是否有检查 null / undefined 的最短方法?

【问题讨论】:

  • “不起作用”是什么意思?
  • JavaScript null coalescing operator 目前只是一个提议,因此语言中没有内置任何内容。
  • TypeScript 中还没有安全的空合并运算符。

标签: typescript


【解决方案1】:

JavaScript 中目前没有null coalescing operator。有不同的方法可以绕过它(比如嵌套的三元类型)。如果您愿意将辅助类型和函数放入某个库中,您可以这样做:

type MyResponse = {
  address?: null | {
    country?: null | {
      name?: null | string
    }
  }
}

let response: MyResponse = Math.random() < 0.5 ? 
  { address: { country: { name: "France" } } } : { address: null }

var addressCountryName = nullSafe(response, r => r.address.country.name); 
// string | undefined | null

nullSafe() 的定义如下:

interface NullSigil {
  [k: string]: NullSigil;
}

// phantom property to recover T from NullSafe<T>
type OriginalTypeKey = "***originalType***"

type IsNullable<T, Y, N> = null extends T ? Y :
  undefined extends T ? Y : N;

type NullSafe<T, N = NullSigil> = Record<OriginalTypeKey, T> & (
  T extends object ? {
    [K in keyof T]-?: NullSafe<T[K], N>
  } : IsNullable<T, NonNullable<T> | N, T>
)

type NullUnsafe<T> =
  T extends Record<OriginalTypeKey, infer U> ? U :
  T extends NullSigil ? null :
  T

function nullSafe<T, U>(
  val: T,
  fn: <N extends NullSigil>(nullSafeVal: NullSafe<T, N>) => U
): NullUnsafe<U>;
function nullSafe(val: any, fn: (nullSafeVal: any) => any): any {

  const nullSigil: NullSigil = new Proxy({} as NullSigil, { get(t, p, r) { return r } });
  const deproxify = Symbol("deproxify");

  function ns<T>(obj: T): NullSafe<T>;
  function ns(obj: any) {
    if ((typeof obj === "undefined") || (obj === null)) return nullSigil;
    if (typeof obj !== "object") return obj;
    return new Proxy(obj, { get(t, p, r) { return (p === deproxify) ? t : (p in t) ? ns(t[p]) : nullSigil } });
  }

  const ret: any = fn(ns(val));

  if (ret === nullSigil) return null;
  if (typeof ret !== "object") return ret;
  return ret[deproxify];
}

是的,一团糟,这就是为什么我说要把它塞进图书馆。它通过创建一个Proxy 来工作,即使它本质上是空的,它也总是允许你深入研究属性。

无论如何,这是一种选择。祝你好运!

【讨论】:

    【解决方案2】:

    正如其他人所提到的,null coalescing operator 仍处于提案阶段,因此您无法使用 ?. 访问属性您目前唯一的选择是使用 if 语句或三元组进行深度嵌套,或者考虑使用类似 @987654324 的方法来自 lodash 的 @ 方法可以让你这样做:

    let addressCountryName = get(response, 'address.country.name');

    如果对象路径的任何部分为 null 或未定义,则返回 name 或 undefined 的值。

    这是一个 CodeSandbox,其中包含使用 lodash 的示例: https://codesandbox.io/s/mm46wkr058

    【讨论】:

      【解决方案3】:

      !是 Typescript 中的非空断言运算符。 ?用于 Angular html 模板。

      【讨论】:

      • 非空断言运算符所做的事情与 OP 想要的完全不同。
      • 不确定你的意思......你的意思是使用这个:response.address!.country!.name?不工作...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2020-09-21
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      相关资源
      最近更新 更多