2

I am defining the following Typescript interface. clickCustomButton1 should return nothing but I am not sure how to specify that.

interface IButtonTemplate {
    clickCustomButton1: (); // How can I say this should return nothing?
    // more code here
}

I use this in my code like this:

clickCustomButton1: null

then later:

newTopicTests = () => {
}

clickCustomButton1 = this.newTopicTests();

It's giving me an error saying:

Error   2   Cannot convert 'void' to '() => boolean'

Can someone give me an idea what I am doing wrong? What I think I need to do is to specify that clickCustomButton1 and also the newTopicTests do not return anything. But how can I do that with Typescript?

1
  • user2864740 - I am sorry but I am not sure what I understand what you mean. I added a line to my question asking if someone can show me how I can do this and make it work when anything I assign to clickCustomButton1 should meet the criteria that it return nothing. Commented Sep 26, 2014 at 10:14

1 Answer 1

2

The problem is because the lambda () => {} is typed as (): void because it returns nothing and thus has no [other] type inferred.

Thus, given f = () => {}, the expression f() is also typed as void - but clickCustomButton1 must return a boolean as it is declared.

Compare when using the following lambda, which is typed as (): boolean, which is now type-valid:

newTopicTests = () => true

Another way to see this problem is to write the original code as:

newTopicTests = (): boolean => {}

(This will also fail to compile, but will show the error closer to where it the source.)


After the updates to the question..

To declare a method in an Interface to return nothing, use:

clickCustomButton1(): void;

To declare a member which has the type of (): void, use

clickCustomButton1: () => void;

Also note that null is something whereas void represents nothing.

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

Comments

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.