带有泛型的 Typescript 箭头函数的语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32308370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the syntax for Typescript arrow functions with generics?
提问by Andreas Frische
The typescript handbook currently has nothing on arrow functions. Normal functions can be generically typed with this syntax: example:
打字稿手册目前没有关于箭头函数的内容。可以使用以下语法对普通函数进行一般类型化:例如:
function identity<T>(arg: T): T {
return arg;
}
What is the syntax for arrow functions?
箭头函数的语法是什么?
采纳答案by Andreas Frische
The language specification says on p.64f
语言规范在 p.64f 上说
A construct of the form < T > ( ... ) => { ... } could be parsed as an arrow function expression with a type parameter or a type assertion applied to an arrow function with no type parameter. It is resolved as the former[..]
< T > ( ... ) => { ... } 形式的构造可以解析为带有类型参数的箭头函数表达式或应用于没有类型参数的箭头函数的类型断言。它被解决为前者[..]
example:
例子:
// helper function needed because Backbone-couchdb's sync does not return a jqxhr
let fetched = <
R extends Backbone.Collection<any> >(c:R) => {
return new Promise(function (fulfill, reject) {
c.fetch({reset: true, success: fulfill, error: reject})
});
};
回答by jbmilgrom
The full example explaining the syntax referenced by Robin... brought it home for me:
解释Robin 引用的语法的完整示例……为我带回家:
Generic functions
通用函数
Something like the following works fine:
类似以下的工作正常:
function foo<T>(x: T): T { return x; }
However using an arrow generic function will not:
但是,使用箭头通用函数不会:
const foo = <T>(x: T) => x; // ERROR : unclosed `T` tag
Workaround: Use extends on the generic parameter to hint the compiler that it's a generic, e.g.:
解决方法:在泛型参数上使用扩展来提示编译器它是泛型的,例如:
const foo = <T extends unknown>(x: T) => x;
回答by Robin Luiten
I found the example above confusing. I am using React and JSX so I think it complicated the scenario.
我发现上面的例子令人困惑。我正在使用 React 和 JSX,所以我认为它使场景复杂化。
I got clarification from TypeScript Deep Dive, which states for arrow generics:
我从TypeScript Deep Dive得到了澄清,其中说明了箭头泛型:
Workaround: Use extends on the generic parameter to hint the compiler that it's a generic, this came from a simpler example that helped me.
解决方法:在泛型参数上使用 extends 来提示编译器它是泛型的,这来自一个帮助我的更简单的例子。
const identity = < T extends {} >(arg: T): T => { return arg; }
回答by mb21
If you're in a .tsx
file you cannot just write <T>
, but this works:
如果你在一个.tsx
文件中,你不能只写<T>
,但这有效:
const foo = <T, >(x: T) => x;
As opposed to the extends {}
hack, this hack at least preserves the intent.
与extends {}
hack相反,这个 hack 至少保留了意图。
回答by Harshit Singhai
This works for me
这对我有用
const Generic = <T> (value: T) => {
return value;
}
回答by Michal Filip
while the popular answer with extends {}
works and is better than extends any
, it forces the T
to be an object
虽然流行的答案 withextends {}
有效并且比 好extends any
,但它迫使 theT
成为一个对象
const foo = <T extends {}>(x: T) => x;
to avoid this and preserve the type-safety, you can use extends unknown
instead
为避免这种情况并保持类型安全,您可以extends unknown
改用
const foo = <T extends unknown>(x: T) => x;
回答by Roy Art
I to use this type of declaration:
我使用这种类型的声明:
const identity: { <T>(arg: T): T } = (arg) => arg;
It allows defining additional props to your function if you ever need to and in some cases, it helps keeping the function body cleaner from the generic definition.
如果您需要,它允许为您的函数定义额外的道具,并且在某些情况下,它有助于使函数体与通用定义保持清洁。
If you don't need the additional props (namespace sort of thing), it can be simplified to:
如果你不需要额外的道具(命名空间之类的东西),它可以简化为:
const identity: <T>(arg: T) => T = (arg) => arg;
回答by Bear
so late, but with ES6 no need extends it still work for me.... :)
这么晚了,但是使用 ES6 不需要扩展它仍然对我有用....:)
let getArray = <T>(items: T[]): T[] => {
return new Array<T>().concat(items)
}
let myNumArr = getArray<number>([100, 200, 300]);
let myStrArr = getArray<string>(["Hello", "World"]);
myNumArr.push(1)
console.log(myNumArr)