How can I add optional named parameters to a TypeScript function parameter? -
i'd able define method accepts arrow function parameter. function parameter should able define 0 or more named parameters of own. how can this?
i've tried
public dosomething(fn: () => any) {} and
public dosomething(fn: (...args) => any) {} but both throw "supplied parameters not match" error when seek phone call follows:
dosomething((test: string) => {}) (note type , number of parameters function may vary on each usage, can't set type(s) on original method.)
i can supply parameters using ...args syntax within passed function, able type , name specific parameters.
is there way this?
two options. reasons type safety. if work through statements below should clear.
fix callback:if function expects phone call callback 0 or more arguments, callback must okay (i.e. have optional arguments) i.e.
function dosomething(fn: () => any) {} // arguments need optional dosomething((a?:string)=>{}); same :
function dosomething(fn: (...args:string[]) => any) {} // arguments need optional dosomething((a?:string)=>{}); fix callback signature: if going phone call callback 'n' arguments, specify names in signature. callback take or ignore these. eg. callback ignores them:
// specify number of arguments function dosomething(fn: (ar1:string,arg2:string) => any) {} dosomething(()=>{}); typescript
No comments:
Post a Comment