6

I'm trying to write a template function, that is an arrow function, and assign it to a const variable,

This is supposed to be of the form const method: MethodType<T> = <T>(...) => { ... }

But it complains when I try to type the variable method. Below is a snippet of the code:

export type KeyMethod<T> = (data: T) => any;

export interface DiffResult<T> {
    additions: T[];
    updates: T[];
    deletes: T[];
};

export type DiffMethod<T> = (oldState: T[], newState: T[]) => DiffResult<T>;

                     it complains about this template
                                   vvv
export const diffMethod: DiffMethod<T> = <T>(oldState: T[], newState: T[]) => {
    return {
        additions: [],
        updates: [],
        deletes: []
    }
};

Is there any way to do this? maybe I'm failing to follow the syntax, but I haven't found a similar example for that.

1
  • 2
    In typescript it's called generic and not template. You cannot use the generic constraint there because it's "unbound", only like this: const diffMethod = <T>(oldState: T[], newState: T[]) ... . Commented May 10, 2017 at 15:12

1 Answer 1

12

As implied in a comment by Nitzan Tomer, you should write like the following.

export interface DiffResult<T> {
  additions: T[];
  updates: T[];
  deletes: T[];
}

export type DiffMethod = <T>(oldState: T[], newState: T[]) => DiffResult<T>;

export const diffMethod: DiffMethod = <T>(oldState: T[], newState: T[]) => {
  return {
    additions: [],
    updates: [],
    deletes: []
  };
};
Sign up to request clarification or add additional context in comments.

1 Comment

was there update?

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.