如何在TypeScript中确定变量类型?

时间:2018-03-13 22:31:20

标签: typescript types

我有这个逻辑,

.switchMap((data: ISearchModalPayload | number ) => {..... }

其中data是接口对象或数字。

如何在继续我的例程之前确定数据类型?

我尝试了以下这个post,但没有效果:

  • if (obj instanceof C) {}
  • Object.getClass()
  • .isInstance

2 个答案:

答案 0 :(得分:1)

if (typeof obj === 'number') {
  // do stuff
} else if (typeof obj === 'object') {
  // do stuff
}

答案 1 :(得分:1)

您在寻找typeof运营商吗? E.g。

typeof obj === "number"