检查Typescript中对象的属性是否为null

时间:2019-01-24 19:52:16

标签: typescript

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

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

地址和国家/地区都可以为空...我也尝试过:

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

这似乎不起作用...

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

3 个答案:

答案 0 :(得分:3)

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来起作用,即使该属性本质上为null,它也始终允许您向下钻取属性。

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

答案 1 :(得分:2)

正如其他人所提到的,null coalescing operator仍处于提议阶段,因此您无法使用?.访问属性,当前唯一的选择是对if语句或三元进行深层嵌套或考虑某些事项就像lodash的get方法一样,您可以这样做:

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

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

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

答案 2 :(得分:0)

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

相关问题