英文:
How to get the return type of a generic function inside a class with TypeScript?
问题
使用Angular的类型化表单,我想获取FormBuilder.group<T>()
的返回类型(R),以便可以像这样使用R:FormGroup<R>
。
为此,我尝试了以下代码:
type R = ReturnType<FormBuilder['group<T>']>;
但这是无效的TypeScript。
是否有一种方法可以让R成为特定泛型参数T的group函数的返回类型?
英文:
With Angular's typed forms, I want to get the return type (R) of FormBuilder.group<T>()
such that I can use this R like this: FormGroup<R>
.
For this I tried:
type R = ReturnType<FormBuilder['group']<T>>;
Which is invalid Typescript.
Is there a way to get R to be the return type of the group function for a specific generic parameter T?
答案1
得分: 0
以下是翻译好的部分:
"这不可能做到(也许是疏忽了?)而不首先降到表达级别:"
declare const _group: FormBuilder["group"];
type R = ReturnType<typeof _group<{ foo: "bar" }>>; // ok
<details>
<summary>英文:</summary>
It's not possible to do this (maybe it's an oversight?) without dropping to the expression level first:
```ts
declare const _group: FormBuilder["group"];
type R = ReturnType<typeof _group<{ foo: "bar" }>>; // ok
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论