简体   繁体   English

如何区分类型和功能?

[英]How to tell apart Type from Function?

I have an argument of type 我有一个类型的参数

((element: any) => Type<any>) | Type<any>

and if it is a Function i need to call it, other wise just use the Type as-is 如果它是一个函数,我需要调用它,否则请按原样使用Type

getType(element: any, type: ((element: any) => Type<any>) | Type<any>): Type<any> {
    return isFunctionNotType(type) ? type(element) : type;
}

Unfortunately I am not sure how to write the isFunctionNotType method. 不幸的是,我不确定如何编写isFunctionNotType方法。 There is an isType method in @angular/core/src/type but whenever I try using that I get the following error during compilation @angular/core/src/type有一个isType方法,但是每当我尝试使用它时,在编译过程中都会出现以下错误

ERROR in C:/Users/xxxxxx/xxxxxx/xxxxxx/dist/xxxxxx/fesm5/angular-utils.js
Module not found: Error: Can't resolve '@angular/core/src/type' in 'C:\Users\xxxxxx\xxxxxx\xxxxxx\dist\xxxxxx\fesm5'

Is there some way to get this working without having to break up the type variable into two? 是否有某种方法可以使此工作有效而不必将type变量分解为两个?

EDIT: I should point out since people seem to be missing the point here. 编辑:我应该指出,因为人们似乎在这里遗漏了要点。 In angular Type extends Function , and at run-time you will not be able to tell them apart just by doing an instanceof or something like that. 在angular Type扩展Function ,在运行时,您将无法仅通过执行instanceof或类似操作来区分它们。

(typeof callback === "function") (typeof回调===“函数”)

I would check if its a function, then do something for the other type with an 'else'. 我会检查它是否有功能,然后用“ else”为其他类型做一些事情。

type TypeFn<T> = (element: T) => Type<T>;

function isFunctionNotType<T>(type: TypeFn<T> | Type<T>): type is TypeFn<T> {
    return typeof type === 'function';
}

function getType<T>(element: T, type: TypeFn<T> | Type<T>): Type<T> {
    return isFunctionNotType(type) ? type(element) : type;
}

isFunctionNotType return type type is TypeFn<T> give a boolean AND will infer the return type. isFunctionNotType返回类型的type is TypeFn<T>给出一个布尔值,并将推断返回类型。

Note that I added generics, it may be inappropriate so remove them in this case. 请注意,我添加了泛型,可能不合适,因此在这种情况下将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉Typescript在这种情况下*函数的返回类型永远不会为null - How to tell Typescript that *in this instance* a function's return type is never null 除了弹簧靴的作用外,Jhipster 如何支持百里香? - How Jhipster supports thymeleaf apart from what spring boot does? 除了 appendTo="body" 之外,如何避免 p-dropdown 在 body 滚动时关闭 - How to avoid p-dropdown from closing on body scroll apart from appendTo="body" 除了电子邮件和密码之外,如何在firebase中添加用户的额外个人资料信息? - how to add user's extra profile information apart from email and password in firebase? 如何停止 mat-autocomplete 以将自定义用户输入值与给定选项分开? - How to stop mat-autocomplete to take custom user input values apart from given options? 从 - “createAction” function 获取类型 - Get the type from a - "createAction" function Angular 7:如何告诉Angular编译器/构建器不要包含来自js的数据? - Angular 7 : how to tell Angular compiler / builder not to include data from js? 如何告诉Angular 2停止自动设置Content-Type标头? - How can I tell Angular 2 to stop setting Content-Type headers automatically? 如何判断是否经过身份验证 - How to tell if authenticated 我想知道如何在 highcharts 中使用 onclick() 函数和符号我在 angular 中使用它。请告诉我 - I want to know how to use the onclick() function and symbol in highcharts Iam using it in angular.Tell me please
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM