简体   繁体   English

使用 Typescript,如何键入功能性 True 函数?

[英]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.

相关问题 如何使用打字稿键入此回调函数 - How do I type this callback function using typescript Typescript ESLint 错误 - 使用 React 功能组件时如何输入子项? - Typescript ESLint Error - How can I type children when using react functional components? 如何编写React Functional Component + TypeScript的函数重载? - How do I write React Functional Component + TypeScript's function overloading? 在使用通用初始值设定项 function 时,如何让 typescript 正确推断类型? - How do I have typescript have the type properly infered when using a generic initializer function? 如何在 TypeScript 中使用具有派生参数类型的通用 args 变量调用 function? - How do I call a function using a generic args variable with a derived Parameters type in TypeScript? 在TypeScript中,如何键入函数的参数而不是返回值? - In TypeScript, how do i type a function's arguments but not the return value? 如何在打字稿中为这个咖喱函数分配正确的类型签名? - How do I assign the right type signature to this curried function in typescript? 如何在打字稿中的类方法上强制执行函数类型接口? - How do I enforce a function type interface on a class method in typescript? 样式组件 + Typescript:如何键入 css 助手 function? - Styled components + Typescript: How do I type the css helper function? 如何使用 typescript 在 function 参数中扩展 object 类型? - How do I extend object type inside function param with typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM