Using Typescript, how do I type the functional True function?

241 views Asked by At

For background, I am going through the "Flock of Functions" and doing my best to convert these Javascript examples to typed Typescript. See https://github.com/glebec/lambda-talk/blob/master/src/index.js#L152 for reference. The True function returns the first curried argument, and ignores the second.

Consider the following Typescript code:

interface ElsFn<T> {
  (els: unknown): T;
}

interface True extends Function {
  <T>(thn: T): ElsFn<T>;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const T: True = (thn) => (_els) => thn;

console.log(T('true')('false'));

Assuming I want to keep the "explicit-function-return-type" rule, how do I get rid of the ESLint disable comment? In other words, I want to properly type the True function.

My editor tells me the problem is with the (_els) => thn portion of the code. It needs to be typeed somehow.

enter image description here ]

What can I do to set the return type or otherwise get this thing properly typed so that I don't need to disable the ESLint rule?

2

There are 2 answers

1
x1n13y84issmd42 On BEST ANSWER

You still need to specify generic arguments and return types:

const T: True = <T_>(thn) => <T_>(_els):T_ => thn;
1
Yuna On
(_els): boolean => thn;

Could it be working for your case?