0

I keep getting an error during compilation: error TS2339 Property 'customMethod' does not exist on type 'Action[]'

These are my interfaces:

export interface Action {
    Name: string;
    someFunc(): void;
}

export interface ActionCollection {
    Actions: Action[];
}

Then in my code I'm trying to use a method that has not been described YET in the interface but it is available at runtime. This method is is available from the Actions array within ActionsCollection, just like a native .length property.

let myAC: ActionCollection = new ActionCollection( stuff );
myAC.Actions.customMethod(); // Note that it is attached to Actions

My question is how do I define it in the interfaces?

I've tried something like this, but I got errors:

export interface Action<> {
    customMethod(): any;
}
1
  • You shouldn't be able to do new ActionCollection as ActionCollection is an interface, you don't get an error there? Commented Jul 1, 2016 at 12:20

1 Answer 1

2

If you want your actions array to have this method then you either need to add it to the Array interface:

interface Array<T> {
    customMethod(): void;
}

And then all arrays will have it:

let a = [];
a.customMethod();

But that's probably not what you're after, instead just define your own array:

export interface Action {
    Name: string;
    someFunc(): void;
}

export interface ActionsArray extends Array<Action> {
    customMethod(): void;
}

export interface ActionCollection {
    Actions: ActionsArray;
}

You say that you already implemented the actual function, so this should be enough.

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.