I am trying to build a shape
where key is string and value is function. This works fine for regular functions but gives error for async functions. Following is what I am trying to do
const type TAbc = shape(
'works' => (function (): string),
'doesnt_work' => (async function(): Awaitable<string>)
);
This results in error A type specifier is expected here.Hack(1002)
Encountered unexpected text async, was expecting a type hint.Hack(1002)
Is this illegal in Hacklang? If yes then would like to know why?
async function (): T
is illegal in Hacklang. The suggested way to do this is by defining it in the return type, Awaitable<T>
.
So to get this working we can do this
const type TAbc = shape(
'sync' => (function (): string),
'async' => (function(): Awaitable<string>)
);
And while initialising an instance of this type we can internally call an async function. For eg:
$abc = shape(
'async' => function(): Awaitable<string> {
return someAsyncFunction();
});