ts 拓展已有类的方法

打算往已有类Trigger 增加addCond方法,做一个参数转换,调用原先的addCondition方法

// ext.ts
import { Trigger } from "solar/w3ts/handles/trigger";
declare module "solar/w3ts/handles/trigger" {
    interface Trigger {
        addCond(condition: () => boolean): void
    }
}
if (!Trigger.prototype.addCond) {
    Trigger.prototype.addCond = function(condition: () => boolean) {  
        BJDebugMsg('123')
        this.addCondition(Condition(() => condition()))
    }  
}

值得注意的时候,传参的时候,必须传() => condition,直接传condition,TSTL不支持,会抛这个错:

 error TSTL: Unable to convert function with a 'this' parameter to function 'func' with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.

发表于 2022.02.19