简体   繁体   English

如何使用 generics 定义 Typescript 类型

[英]How to define a Typescript type using generics

Let's say I have the following interface defining an object:假设我有以下定义 object 的接口:

interface ExampleInterface {
    a: string:
    b: number;
    c: AsyncFunction<boolean>
}

where AsyncFunction is defined by:其中 AsyncFunction 定义为:

type AsyncFunction<T> = () => Promise<T>;

This currently works well if the function supplied to c: has no parameters, but causes a TS2322 compilation error if the function has any parameters.如果提供给 c 的 function: 没有参数,则当前运行良好,但如果 function 有任何参数,则会导致 TS2322 编译错误。

The idea is that <T> is used to define the return type within the Promise of the function.这个想法是<T>用于定义 function 的 Promise 内的返回类型。 The function may or may not need its own parameters, but it must provide the specified T return type within a Promise. function 可能需要也可能不需要自己的参数,但它必须在 Promise 中提供指定的 T 返回类型。

My question is, is there a way to modify the type definition so that any function with zero or any number of parameters of any fashion can be used in c :?我的问题是,有没有办法修改类型定义,以便在 c 中使用具有零或任意数量参数的任何function :?

I've tried to understand infer but all of my attempts to use it (and it may not be what I'm looking for) have ended up with c : having an (any) return type which is not what I want.我试图理解推断,但我所有使用它的尝试(它可能不是我想要的)都以c结束:有一个(任何)返回类型,这不是我想要的。 I've also tried using the following with no success.我也尝试过使用以下内容但没有成功。

type AsyncFunction<T> = (...args: any[]) => Promise<T>;

Thanks in advance.提前致谢。

Your example "tried the following with no success works perfectly":您的示例“尝试了以下没有成功的完美工作”:

Playground 操场

Thanks to @Rubydesic making me walk through everything to provide more details, I realised I was being an idiot.感谢@Rubydesic 让我遍历所有内容以提供更多细节,我意识到我是个白痴。

The reason it wouldn't compile had nothing to do with the type definition - that didn't actually need changing.它无法编译的原因与类型定义无关 - 实际上不需要更改。

The issue was that with no parameters, I was simply passing the method name into c:问题是没有参数,我只是将方法名称传递给 c:

async function foo(): Promise<boolean> {
// Body omitted
}

const x: ExampleInterface = {
a: 'a',
b: 0,
c: foo
}

Of course, this worked, but when the method had parameters I was then passing those in (which because of the need for brackets would be attempting to execute it, not passing it in - the part I failed to realise after a long day - talk about missing the obvious).当然,这行得通,但是当方法有参数时,我将这些参数传入(因为需要括号,所以会尝试执行它,而不是传入它 - 经过漫长的一天后我未能意识到的部分 - 谈话关于错过明显的)。 I was doing:我在做:

async function bar(a: number): Promise<boolean> {
// Body omitted
}

const x: ExampleInterface = {
a: 'a',
b: 0,
c: bar(0)
}

When I should have been doing当我应该做的时候

async function bar(a: number): Promise<boolean> {
// Body omitted
}

const x: ExampleInterface = {
a: 'a',
b: 0,
c: () => bar(0)
}

Answer posted as a painful reminder to take a break every so often.发布的答案是一个痛苦的提醒,要经常休息一下。 In 10 years, I don't think I've ever been quite so blind to the problem! 10 年来,我认为我从未对这个问题如此盲目!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM