HarmonyOS 泛型类型判断?

type TestType<T\> = T extends image.ImageSource;

上面的写法不支持,怎么判断泛型的类型

阅读 481
1 个回答

可使用instanceof 判断某个对象是否是另一个对象的实例,typeof用于检测对象的类型,可参考如下代码:

function exampleFunction<T>(arg: T): void {
  if (typeof arg === 'string') {
    console.log('T is a string');
  } else if (typeof arg === 'number') {
    console.log('T is a number');
  } else {
    console.log('T is of another type');
  }
}


export function JumpByObject(params: NewsListBean | JumpData) {
  if (params instanceof NewsListBean) {
    console.log('11111111111111');
  }else if(params instanceof JumpData){
    console.log('22222222222222');
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏