[英]Typescript: typing only the arguments or the return type of a function
I'm having several functions that accept different arguments but all of them return a function with the same signature: 我有几个函数接受不同的参数,但它们都返回一个具有相同签名的函数:
const createSum5 = () => (c: number) => c + 5;
const createMultiplyN = (n: number) => (c: number) => n * c;
const createWordsSum = (word: string) => (c: number) => word.length + c;
The return type is always (c: number) => number
. 返回类型始终是
(c: number) => number
。 Is there a way that I can type this return value for all the functions, without typing the argument? 有没有办法可以为所有函数键入此返回值,而无需键入参数? So that I could short them to this without losing type safety:
这样我就可以在不丢失类型安全的情况下缩短它们:
createSum5: /* Something here */ = () => c => c + 5;
I've tried these, but: 我试过这些,但是:
1.This solution loses type-safety: type NumericArg = (...args: any) => (c: number) => number
; 1.此解决方案失去了类型安全性:
type NumericArg = (...args: any) => (c: number) => number
;
2.I could write the function signature for every function in advance: 2.我可以事先为每个函数编写函数签名:
type Nfunction = (c: number) => number;
type createSum5Type = () => Nfunction;
...
const createSum5: createSum5Type = () => c => c + 5;
But that would be tedious. 但那将是乏味的。 I want Typescript to automatically infer the arguments as it would do if I don't specify the types.
如果我不指定类型,我希望Typescript自动推断参数。
Is there any other way? 还有其他方法吗? Can the opposite be done (specifying functions with the same signature but let Typescript infer the return type?).
可以相反完成(指定具有相同签名的函数,但让Typescript推断返回类型?)。
EDIT #1: Here's an example when I speak about the opposite: 编辑#1:这是我说的相反的例子:
const func1: /* Something here */ = num => '#'.repeat(5);
const func2: /* Something here */ = num => num * num;
Both func1 and func2 have the same signature (num: number)
but different return types. func1和func2都具有相同的签名
(num: number)
但返回类型不同。 I want Typescript to infer that every time I call func1 the return type is a string
, but with func2
the return type is a number. 我希望Typescript推断每次调用func1时返回类型都是一个
string
,但是对于func2
,返回类型是一个数字。
EDIT #2: 编辑#2:
My use case is not relevant, but I'll add it anyway. 我的用例不相关,但无论如何我都会添加它。 I'm developing a React-redux application using Thunks.
我正在使用Thunks开发一个React-redux应用程序。 Every thunk always returns a function with signature
(dispatch, getState) => void
but they may accept different parameters. 每个thunk总是返回一个带签名
(dispatch, getState) => void
的函数,但它们可以接受不同的参数。 After some research this is the less verbose version that I could find: 经过一些研究,这是我能找到的不那么冗长的版本:
const onFetchUser = (userIds: number[]) : ThunkFunction = dispatch => { ... }
. const onFetchUser = (userIds: number[]) : ThunkFunction = dispatch => { ... }
。
If there was a way to allow Typescript to infer the arguments but let me set the return type (which is my question here), I could make it easier to read like this: 如果有一种方法允许Typescript推断参数,但让我设置返回类型(这是我的问题),我可以让它更容易阅读:
const onFetchUser: /* something here */ = (userIds: number[]) => dispatch => {...}
. const onFetchUser: /* something here */ = (userIds: number[]) => dispatch => {...}
。
Not sure if it is the only option, but one option is to use a helper function that has a generic parameter that will capture the actual type of the function passed in but that will also enforce a return type of (c: number)=> number
. 不确定它是否是唯一的选项,但是一个选项是使用辅助函数,该函数具有一个通用参数,该参数将捕获传入的函数的实际类型,但也将强制执行返回类型
(c: number)=> number
。
function fn<T extends (...a: any[]) => (c: number) => number>(o: T) {
return o;
}
const createSum5 = fn(() => c => c + 5)
const createMultiplyN = fn((n: number) => c => n * c);
const createWordsSum = fn((word: string) => c => word.length + c);
I don't believe another option exists, typescript does not in general allow partial inference for variables (or more specifically constrained inference) this can only be done with a function. 我不相信存在另一个选项,typescript通常不允许对变量进行部分推理(或者更具体地说是约束推理),这只能通过函数来完成。
TypeScript supports the keyword infer
, which would allow you to preserve the types of arguments and/or return types of a function. TypeScript支持关键字
infer
,它允许您保留函数的参数类型和/或返回类型。
For conditional types, it would look like this: 对于条件类型,它看起来像这样:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
There is some info about infer here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html 这里有一些关于推断的信息: https : //www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
Update: 更新:
You can do it just by using a generic function: 您只需使用通用函数即可:
type numFunc<T> = (arg: T) => (c: number) => number;
const createSum5: numFunc<void> = () => (c: number) => c + 5;
const createMultiplyN: numFunc<number> = (n: number) => (c: number) => n * c;
const createWordsSum: numFunc<string> = (word: string) => (c: number) => word.length + c;
const createSumString: numFunc<number> = () => (c: number) => 'Hello'; //error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.