Reputation: 6400
I am new to Typescript I came across the following syntax
interface Foo {
// what is age is here and
// what if it is not optional i.e () => string, age:number;
(): () => string, age?: number;
}
What I have understood Foo is a function interface, any function that implements Foo must return a function that must return a string.
but what age?: number is for.
Upvotes: 2
Views: 434
Reputation: 1074495
It's clearer to understand with a line break, and a semicolon instead of comma (I'm intrigued that the comma is valid, but according to the playground, it is):
interface Foo {
(): () => string;
age?: number;
}
Foo
defines an interface for a function that:
Returns a function that returns a string
, and
Has an optional age
property that's a number
.
More about function types here.
Here's an example of it in use:
interface Foo {
(): () => string,
age?: number;
}
const f: Foo = () => () => "foo";
f.age = 42; // Note this is on the function
Fair to say that's an...interesting...interface. :-)
Upvotes: 4