Home > Enterprise >  "void promiseExpression" in TypeScript
"void promiseExpression" in TypeScript

Time:01-28

Looking at a React Native app in TypeScript, and there is this fragment in a screen file:

export const DetailPageScreen: FC<StackScreenProps<AuthorizedNavigatorParamList, 'Details'>> = observer(({ route, navigation }) => {
  useEffect(() => {
    void (async () => {
      await SomeFunction();
    })();
  }, []);

Help me parse what goes into useEffect. It's a function with a body that goes:

() =>
{
    void (async () => {
      await SomeFunction();
    })();
}

So what's inside the parentheses after void is an async function invocation without an await - an expression that results in a promise, I presume. So what is void doing here, exactly? It boils down to void promiseObject, if I followed this right.

CodePudding user response:

So what is void doing here, exactly?

At runtime, nothing. The void operator simply evaluates what's in front of it and returns undefined.

Without additional context, my best guess is that this is a hint to some linter, most likely typescript-eslint with the no-floating-promises rule.

  • Related