Skip to content

Typescript act() got warning 'await' has no effect on the type of this expression.聽#1276

Closed

Description

@qaynam

Describe the bug

when i use act with typescript , got warning like 馃憞

image

image

Steps to Reproduce

@testing-library/react-native/build/act.d.ts

declare global {
    var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;
}
declare function setIsReactActEnvironment(isReactActEnvironment: boolean | undefined): void;
declare function getIsReactActEnvironment(): boolean | undefined;
- declare const act: (callback: () => void) => void;
+ declare const act: <T>(callback: () => T) => T;
export default act;
export { setIsReactActEnvironment as setReactActEnvironment, getIsReactActEnvironment, };

Versions

image

Activity

  1. mdjastrzebski commented on Jan 7, 2023

    @mdjastrzebski
    Member

    Looking at @types/react-test-render theirs act has following type:

    export function act(callback: () => Promise<VoidOrUndefinedOnly>): Promise<undefined>;
    

    As far as I understand the intention of async act is to allow for waiting for Promise returned by callback to resolve, but not to use it's return value in any way. We could actually tweak our typings to match @types/react-test-render ones.

    RTL does hits in the following way:

    /**
     * Simply calls ReactDOMTestUtils.act(cb)
     * If that's not available (older version of react) then it
     * simply calls the given callback immediately
     */
    export const act: typeof reactAct extends undefined
      ? (callback: () => void) => void
      : typeof reactAct

    @qaynam Would such solution satisfy your use case?

  2. trajano commented on Jan 17, 2023

    @trajano

    I'm not sure how you'd declare it in the act.ts file but the generated act.d.ts file should have these two lines

    declare const act: (callback: () => void) => void;
    declare const act: (callback: () => Promise<VoidOrUndefinedOnly>): Promise<undefined>;
    

    You have to also ensure that the linters will allow

    await act(() => Promise.resolve());
    await act(async () => {});
    

    Since these are valid cases where the useEffect performs an async operation that needs to be waited on.

  3. trajano commented on Jan 20, 2023

    @trajano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions