在Flow中使用星号(*)类型有什么用,在TypeScript中有什么用呢? [英] What is the use of asterisk (*) type in Flow and what is the equivalent of that in TypeScript?
问题描述
首先,我大多熟悉TypeScript。 Flow在许多方面看起来与TS非常相似,但我最近偶然发现了一个星号(*)类型。起初,我认为它是any的同义词,但是现在,在阅读了Flow的一些发行说明后,我发现它并非如此。我浏览了所有官方文档,但未能找到*的任何用法。
First of all, I am mostly familiar with TypeScript. Flow looks very similar to TS in many ways, but I recently stumbled upon an asterisk (*) type. At first, I thought it was a synonym for "any", but now, after reading some of the release notes for Flow, I see that it's not. I skimmed through all the official docs and was not able to find any usage of "*".
那么,它是什么以及何时使用它?但是,什么是TypeScript中的直接等价?
So, what is it and when to use it? But also, what would be a direct equivalent of that in TypeScript?
推荐答案
编辑:因为我最初写这个答案,我有据悉, *
出现在模块边界时是不安全的。我不建议使用它,它可能会在将来被删除。
Since I originally wrote this answer, I have learned that *
is unsafe when it appears at module boundaries. I can't recommend using it, and it may be removed in the future.
它只是告诉Flow推断一个类型参数,而不是让你明确地写出来:
It just tells Flow to infer a type parameter, rather than making you write it out explicitly:
function foo(): Array<*> {
return [5];
}
// Flow issues an error:
// 2: return [5];
// ^ number. This type is incompatible with
// 10: (foo(): Array<string>);
// ^ string
(foo(): Array<string>);
(尝试流程)
它不同于任何
- - 任何
都是不安全的类型,所以如果用替换
在这个例子中,Flow不会给你任何错误。你可以用 *
任何 number
替换它,Flow会给你一个类似的错误。
It is different from any
-- any
is an unsafe type, so if you replaced *
with any
in this example, Flow would not give you any errors. You could replace it with number
and Flow would give you a similar error.
这篇关于在Flow中使用星号(*)类型有什么用,在TypeScript中有什么用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!