Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeScript如何为泛型函数创建泛型类型别名?_Typescript_Generics - Fatal编程技术网

TypeScript如何为泛型函数创建泛型类型别名?

TypeScript如何为泛型函数创建泛型类型别名?,typescript,generics,Typescript,Generics,给定一个类型化的泛型函数,我想为该函数创建一个泛型别名,但似乎不行。换句话说,这是行不通的: // foo.tsx function foo<T>(arg: T): T { return arg } type FooT = typeof foo // works, but alias isn't generic: <T>(arg: T) => T type FooParametersT = Parameters<typeof foo> // s

给定一个类型化的泛型函数,我想为该函数创建一个泛型别名,但似乎不行。换句话说,这是行不通的:

// foo.tsx
function foo<T>(arg: T): T {
  return arg
}

type FooT = typeof foo  // works, but alias isn't generic: <T>(arg: T) => T
type FooParametersT = Parameters<typeof foo>  // sure: [unknown]
type FooReturnT = ReturnType<typeof foo>  // no problem: unknown

type GFooT<T,> = typeof foo<T,>  // yikes
type GFooParametersT<T,> = Parameters<typeof foo<T,>>  // nope
type GFooReturnT<T,> = ReturnType<typeof foo<T,>>  // fails
//foo.tsx
函数foo(arg:T):T{
返回参数
}
type FooT=typeof foo//可以工作,但别名不是泛型:(arg:t)=>t
键入FooParametersT=Parameters//sure:[未知]
类型foorReturnt=ReturnType//没有问题:未知
类型GFooT=typeof foo//yikes
类型GFoopParameterST=参数//否
类型GFooReturnT=ReturnType//失败
我真正想做的是在别人的库中获取函数的类型,然后围绕它构建一个接口。例如:

import { useState } from "react"

type UseStateFnT<T,> = typeof useState<T>
const wrapUseState: (toWrap: UseStateFnT<T,>) => UseStateFnT<T,> = …
type GenType<T> = (x: T) => T[];
declare const oops: GenType; // error
declare const genT: GenType<string>; // okay
const strArr = genT("hello"); // string[];
const numArr = genT(123); // error!
从“react”导入{useState}
类型UseStateFnT=类型useState
const wrapUseState:(toWrap:UseStateFnT)=>UseStateFnT=…

这在不重新创建自己的情况下是可能的吗?

TypeScript中有两种不同的泛型:泛型函数和泛型类型。。。看起来您希望编译器为您将一个转换为另一个,这是不直接支持的


要明确的是:

泛型类型具有类型参数,在将其用作特定类型之前需要指定这些参数。例如:

import { useState } from "react"

type UseStateFnT<T,> = typeof useState<T>
const wrapUseState: (toWrap: UseStateFnT<T,>) => UseStateFnT<T,> = …
type GenType<T> = (x: T) => T[];
declare const oops: GenType; // error
declare const genT: GenType<string>; // okay
const strArr = genT("hello"); // string[];
const numArr = genT(123); // error!
这里,
GenFunc
是指泛型函数的特定类型。调用
genF
函数时,它仍然是通用的

泛型函数(包括泛型构造函数)可以看作是泛型类型,而不是泛型类型


这两种类型的泛型是相互关联的,但是TypeScript类型系统没有足够的表达能力来讨论它们是如何关联的。在另一种语言中,您可能可以用另一种语言来定义一个

type GenFunc = forall T, GenType<T>; // not TS, error
您可以验证
GenType2
GenType
的类型相同,如果将
GenFunc
更改为具有一个类型参数的任何通用函数,
GenType2
将相应更改

但我不知道我想推荐谁真正使用这种方法。它并没有真正的规模或组成;如果在一个类型参数中有一个充满泛型函数的对象,并且希望将其转换为一个充满具有指定类型参数的特定函数的对象,那么如果不对每个对象属性执行一次操作,则无法使用此方法从类型系统中获取它


无论如何,希望这有帮助;祝你好运


我想我必须读三遍才能开始摸索,但这反映了我对打字稿细微差别的无知,而不是你的回答,因为你的回答既彻底又写得好。谢谢你!我接受这一点是希望,未来的一些评论或回应将是“这现在可以作为TypeScript…”,这在堆栈溢出的许多TypeScript问题中似乎很常见。
class GenTypeMaker<T> {
    getGenType!: <A extends any[], R>(cb: (...a: A) => R) => () => (...a: A) => R;
    genType = this.getGenType(null! as GenFunc)<T>()
}
type GenType2<T> = GenTypeMaker<T>['genType']
// type GenType2<T> = (x: T) => T[]