Multiple ways to declare a function on a TypeScript interface: how are they different? -
i've seen properties functions declared in multiple ways, illustrated func1
, func2
on typescript interface:
interface thing { func: (arg:string) => number; func2(arg:string): number; }
is there difference between two? there case in use 1 on other?
this playground link seems imply 2 can used interchangeably. there limits this?
is there difference between two
yes.
func: (arg:string) => number;
this version means property. limit when try declare overloads
.
func2(arg:string): number;
this preferred functions means can declare overloads after fact (using open ended nature of interfaces)
seems imply 2 can used interchangeably
that because type compatible. not mean they same thing. see property vs. method below:
example
this should clarify :
interface thing { func: (arg: string) => number; func2(arg:string): number; } interface thing { // overload not permitted func: (arg: number) => string; // error! // overload okay func2(arg: number): string; }
Comments
Post a Comment