0

I would like to implement an interface, but I am having hard time getting the syntax right.

Interface I would like to implement

interface Test { [name : string] : (source : string) => void; } 

If I understood this right, the interface is basically an object with strings as keys and functions as values.

Any help is much appreciated.

Edit : I get several errors, "incorrect implementation of interface", "index signature is missing", etc,

Playground example : void { console.log("test"); }}" rel="nofollow noreferrer">Link

I did not implement the interface, it is from a sdk

1
  • So, what did you try, and what error did you get? And what is this interface supposed to represent? Why did you define it if you don't know how it can be implemented? Commented May 12, 2017 at 15:15

2 Answers 2

1

Just add the index signature to the class:

interface Test { [name: string]: (source: string) => void; } class TestClass implements Test { [name: string]: (source: string) => void; // Add this getLastName(source: string) { console.log("test"); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think a class can implement an interface such as this one.

This is more of a "typed object" interface.

You can type an object to specify that it should have a string as keys and a function taking a string and returning nothing as values.

Like so:

let myObj: Test = { test: (source: string) => { console.log('function returning void'); } }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.