我正在尝试构建一个shape,其中key是字符串,value是函数。这对于常规函数工作得很好,但对于异步函数会产生错误。下面是我正在尝试做的事情
const type TAbc = shape(
'works' => (function (): string),
'doesnt_work' => (async function(): Awaitable<string>)
);这将导致错误A type specifier is expected here.Hack(1002)
Encountered unexpected text async, was expecting a type hint.Hack(1002)
这在Hacklang是非法的吗?如果是,那么想知道为什么?
发布于 2021-08-19 12:10:27
async function (): T在哈克朗是非法的。建议的方法是在返回类型Awaitable<T>中定义它。
因此,为了让它工作,我们可以这样做
const type TAbc = shape(
'sync' => (function (): string),
'async' => (function(): Awaitable<string>)
);在初始化此类型的实例时,我们可以在内部调用异步函数。例如:
$abc = shape(
'async' => function(): Awaitable<string> {
return someAsyncFunction();
});https://stackoverflow.com/questions/68845355
复制相似问题