3

Just starting out with Typescript, and using interfaces for my functions, like so:

interface StringFunction {
    (arg1:string):string
}

I can attach that interface to a function if I declare it as a variable:

let testFunction:StringFunction = function(arg1) {}

but I can't work out how I would (or can?) do it with a named function. This doesn't work:

function testFunction:StringFunction(arg1) {}

is there any way to do this?

3
  • AFAIK interfaces in TypeScript are more like an interface in C# or Java - it defines an object API, therefore StringFunction is not a function name, its an interface you can implement. In other words, if you had interface StringFunction with the function taking string a1, you wouldn't call it like: StringFunction(arg1), but you would have to instantiate a class which implements the StringFunction interface and then: obj.func(arg1) Commented Sep 16, 2016 at 14:25
  • As far as I can tell, TypeScript just doesn't have a syntax that allows you to specify an interface for a named function like this. Is there a reason you can't stick with the let testFunction:StringFunction = ... syntax? Commented Sep 16, 2016 at 15:16
  • @smarx I'd assume the reason may be to have a function with name? This is useful during debugging as DevTools show the function name in stack traces. React DevTools also relies on the name of the function for its components in the tree. Commented May 7, 2018 at 13:04

2 Answers 2

8

You can do it but the syntax is a bit different for named functions.

interface StringFunction {
  (arg1:string): string
}

<StringFunction>function testFunction(arg1) {}

Here we cast testFunction to the interface. arg1 will be a string. Make sure the type is before the function keyword.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Any way to do this in tsx files though?
Unfortunately not that I know of since tsx reserves the "<" char for jsx
2

The need to explicitly state which interface is being used is a constraint of a nominal type system. TypeScript is structurally typed, so you don't need a way to explicitly state that your function implements the interface.

For example, the testFunction below is perfectly acceptable where a StringFunction is needed - and the compiler checks that it is.

interface StringFunction {
    (arg1:string):string
}

function testFunction(arg1) { return ''; }

var x: StringFunction = testFunction;

The following example is to illustrate that type checking occurs:

interface StringFunction {
    (arg1:string):string
}

function testFunction(arg1) { return 1; }

var x: StringFunction = testFunction; // Error

In this case, testFunction is not compatible and the compiler issues a warning.

Additional notes:

In strict mode, your original function will indeed need type information (but the answer still applies).

function testFunction(arg1: string) { return ''; }

This is only needed where there is no context to infer the types, so the following works in strict mode even though it looks like it is missing a type annotation. The string type on arg1 is a contextual type.

var x: StringFunction = (arg1) => { return ''; };

8 Comments

This is likely to not work when you have strict flag turned on, because you have to explicitly specify the type for the arguments of the function.
@nilfalse added further explanation for strict mode.
I am the only one thinking that this does not answer the question why it is not possible to add type/interface to the function declaration, without using function expression?
@apieceofbart the beauty of structural vs nominal type systems is that you don't have to tag every implementation of an interface. You can opt to check at the point of consumption, not the point of creation. As far as syntax goes, there isn't a mechanism to mark up a function x with an interface, but there is a mechanism to mark up a const x = function with an interface.
@user75525 Thank you for your comment. I did not understand the first 2 sentences but I will do my homework. I understand that you cannot mark up function x - just wondering if this is not yet implemented or not possible with Typescript at all. My case is that I would like to add types to function components in React without assigning them to variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.