Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript typings for basic currying function

I’m trying to define a basic function with 2 signatures to allow basic currying without including any complicated 3rd party libraries.

This is the basic function, it takes 2 arguments, if the second argument is not provided it returns a function that takes the last argument, then it will execute the function.

function concat(x, y) {
  if (arguments.length === 1) {
    return (_y) => concat(x, _y);
  }
  return x.concat(y);
}

If I use a separate index.d.ts typescript definition file, I can use the following types.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export function concat<T>(x: T, y: T): T;
export function concat<T>(x: T): (y: T) => T;

However, if I overload the function in a ts file I get typedef errors.

export function concat<T>(x: T, y: T): T;
export function concat<T>(x: T): (y: T) => T {
  if (arguments.length === 1) {
    return (_y) => concat(x, _y);
  }
  return x.concat(y); // Type error: TS2304: Cannot find name 'y'.
}

Is there something I am missing in the type definition inline in the ts file?

function concat(x, y) {
  if (arguments.length === 1) {
    return (_y) => concat(x, _y);
  }
  return x.concat(y);
}

// both args provided
console.log(
  concat(['one'], ['two'])
);

// curried
console.log(
  concat(['one'])(['two'])
);

>Solution :

You’ve mixed the overload declarations with the implementation. They need to be separate, regardless whether they are in the same file or not:

export function concat<T>(x: T, y: T): T;
export function concat<T>(x: T): (y: T) => T;
export function concat(x, y?) {
  if (arguments.length === 1) {
    return (_y) => concat(x, _y);
  }
  return x.concat(y);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading