在Typescript中创建`toFunc`通用方法吗?

时间:2018-08-28 08:16:19

标签: typescript

我在Ts中有一个模型,需要填充一些属性。

当前代码简化为:

var  claimSummaryDetails :   {
                  Name :  this._store.selectSync("currentUser") 
                                   .Result
                                   .Family
                                   .find(m => m.MemberID == openClaim.Member_ID)
                }

好的。这就是事情变得复杂的地方。
请注意,我还没有填充name(使用字符串):

enter image description here

我有一个 Object ,其中有许多“名称”选项(englishFirstName / localfirstName

好-我应该显示哪个名字?

这就是为什么我们有一个名为lang的{​​{1}}指示符的原因

所以基本上:

如果isEnglish:boolean,我们应该采取:

isEnglish ==true

否则

{Name: this._store.selectSync("currentUser")
.Result
.Family
.find(m => m.MemberID == openClaim.Member_ID).englishfirstname}

好的,我可以这样做:

{Name: this._store.selectSync("currentUser")
.Result
.Family
.find(m => m.MemberID == openClaim.Member_ID).localFirstName}

但是请注意,它会扫描商店两次,而且时间太长。

好的。

在C#中(很久以前),我创建了一个扩展方法,该方法使我可以接受参数并执行我想做的任何事情,无论哪种类型:

{Name: isEnglish  ? this._store...........englishFirstName : this._store....localFirstName}

结果:1​​

请注意:

public static class A
{
    public static Tout ToFunc<T, Tout>(this T obj, Func<T, Tout> f)
    {
        return f(obj);
    }
}

class Person
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
}

void Main()
{
    bool isEnglish = true;
    Person[] a = new Person[] { new Person() { Prop1 = 1,Prop2=100 }, new Person() { Prop1 = 2,Prop2=200 }, new Person() { Prop1 = 3,Prop2=300 } };

    Console.WriteLine(a.First().ToFunc(whatever => isEnglish ?whatever.Prop1 : whatever.Prop2 ));

}

.ToFunc(whatever => isEnglish ?whatever.Prop1 : whatever.Prop2 whatever中X的值,我可以使用它的道具。

这使我免去了两次扫描商店的麻烦,并创建了其他变量/方法

问题

我尝试使用TS来做到这一点,但没有成功:

X.toFunc

Playground

如何为每种类型修复代码以支持interface Object { toFunc(a:(a)=>any): any; } Object.prototype.toFunc = function (f:(a)=>any){ f(); } var g= [{a:1,b:2}] let res= g.find(d => d.a == 1).toFunc((a) => { if (a.a == 1) return 1; else return 2 }) 的泛型?

换句话说,我该如何实现:

.toFunc(a=>a...)

顺便说一句

一个让我着迷的解决方案是(使用IIFE):

var  claimSummaryDetails :   {
                  Name :  this._store.selectSync("currentUser") 
                                   .Result
                                   .Family
                                   .find(m => m.MemberID == openClaim.Member_ID)
                                   .toFunc(item=>isEnglish ? item.englishFirstName : item.localFirstName)
            }

但是我想知道是否可以创建类型化的 {Name: ((customer) => this.isEng ? customer.englishfirstname : customer.localfirstname)( this._store.selectSync("currentUser") .Result .Family .find(m => m.MemberID == openClaim.Member_ID)) }, 方法。

2 个答案:

答案 0 :(得分:1)

应谨慎考虑在Object上添加方法,但是如果对this参数使用通用参数,则可以推断出类型

interface Object {
    toFunc<T, R>(this: T, fn:(a: T)=> R): R;
}
Object.prototype.toFunc = function <T, R> (this:T, f:(a:T)=> R){

    return f(this);

}


var g= [{a:1,b:2}]
var result = g.find(d => d.a == 1).toFunc(i => this.isEng ? i.a : i.b)

答案 1 :(得分:0)

为什么不简单地在带有条件语句的typescript中使用getter。 一个简单的例子:

class Family {

    isEnglish = false;
    firstName = "FirstName"
    lastName = "LastName"

    constructor(isEnglish: boolean) {
        this.isEnglish = isEnglish;
     }

    get getName(): string{

        if (this.isEnglish) {
            return this.firstName
        } else {
            return this.lastName
        }

    }


}



let family = new Family(true);
alert(family.getName)