[英]Using Typescript, how do I type the functional True function?
For background, I am going through the "Flock of Functions" and doing my best to convert these Javascript examples to typed Typescript.作为背景,我正在浏览“函数群”并尽我所能将这些 Javascript 示例转换为类型化的 Typescript。 See https://github.com/glebec/lambda-talk/blob/master/src/index.js#L152 for reference.请参阅https://github.com/glebec/lambda-talk/blob/master/src/index.js#L152以供参考。 The True function returns the first curried argument, and ignores the second. True 函数返回第一个柯里化参数,并忽略第二个。
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?假设我想保留“显式函数返回类型”规则,我如何摆脱 ESLint 禁用注释? In other words, I want to properly type the True function.换句话说,我想正确键入 True 函数。
My editor tells me the problem is with the (_els) => thn
portion of the code.我的编辑器告诉我问题在于代码的(_els) => thn
部分。 It needs to be typeed somehow.它需要以某种方式输入。
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?我能做些什么来设置返回类型或以其他方式正确输入这个东西,以便我不需要禁用 ESLint 规则?
您仍然需要指定泛型参数和返回类型:
const T: True = <T_>(thn) => <T_>(_els):T_ => thn;
(_els): boolean => thn;
它可能适用于您的情况吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.