Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
qaynam opened this issue Jan 7, 2023 · 3 comments · Fixed by #1293
Closed

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

qaynam opened this issue Jan 7, 2023 · 3 comments · Fixed by #1293

Comments

@qaynam
Copy link

qaynam commented Jan 7, 2023

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

@mdjastrzebski
Copy link
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?

@trajano
Copy link

trajano commented Jan 17, 2023

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.

@trajano
Copy link

trajano commented Jan 20, 2023

I referenced this bug in https://stackoverflow.com/questions/64848604/how-to-resolve-act-in-useeffect/71949404#71949404 for more complete examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants