Reputation: 1649
In Typescript, we can have an interface that represents a function, which looks like so:
interface MyFunction {
(input1: A, input2: B, ...): ReturnType
}
Is it possible to do anything like this with a class? I'd like to have a class that behaves as a function, but I can't find any documentation on that.
I've tried the same format within a class, but that doesn't appear to work.
class Greeter {
private greeting: string = 'hello'
// this doesn't work
(name: string): string {
return `${this.greeting}, ${name}`
}
}
Upvotes: 1
Views: 513
Reputation: 276181
Can I have a class in Typescript behave as a function?
Not at the moment.
There is an issue tracking it : https://github.com/Microsoft/TypeScript/issues/183
There is also the call constructor proposal in ECMAScript : https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md
Upvotes: 1