Skip to content

fix: fixing TypeScript accessibility issue for custom render function #1428

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

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

FelipeESchmidt
Copy link
Contributor

Summary

This Pull Request addresses a problem encountered when trying to use a custom render function in TypeScript. The issue throws an error:
"Exported variable 'customRender' has or is using name 'DebugFunction' from external module '/node_modules/@testing-library/react-native/build/render' but cannot be named."

To resolve this problem, we're exporting the DebugFunction interface from the module it's declared in. This change will allow the TypeScript compiler to recognize the DebugFunction type when it's used in external modules, thus eliminating the naming error that is currently being thrown.

Test plan

The error:

image

With this change the error goes away:

image
image

The error can be solved in the custom render by using RenderResult as the return type, but it'd be nice to work directly. 😀

image

@mdjastrzebski
Copy link
Member

@FelipeESchmidt the error you are getting seems weird, afaik TS should be able to use the type's shape without using it's name. Are you experiencing it with an app or are you creating your own librarary/package?

@codecov
Copy link

codecov bot commented Jul 13, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (0205955) 96.89% compared to head (5175c17) 96.89%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1428   +/-   ##
=======================================
  Coverage   96.89%   96.89%           
=======================================
  Files          65       65           
  Lines        3709     3709           
  Branches      539      539           
=======================================
  Hits         3594     3594           
  Misses        115      115           
Impacted Files Coverage Δ
src/render.tsx 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -136,7 +136,7 @@ function updateWithAct(
};
}

interface DebugFunction {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to add it to package public exports in pure.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the type DebugFunction to the pure.ts export types in my last commit. That's what you meant?

@FelipeESchmidt
Copy link
Contributor Author

@FelipeESchmidt the error you are getting seems weird, afaik TS should be able to use the type's shape without using it's name. Are you experiencing it with an app or are you creating your own librarary/package?

I'm currently using it with an app and the tests gotta use some wrappers, then I've created a custom render to add them around my component that is being tested.

@pierrezimmermannbam
Copy link
Collaborator

Hello @FelipeESchmidt, thanks or submitting this pr! I can't reproduce the issue, I suspect it's due to a difference in typescript config, could you please copy your tsconfig.json file?

@FelipeESchmidt
Copy link
Contributor Author

Sure, follow below:

{
  "compilerOptions": {
    "target": "ESNext",
    "moduleResolution": "node",
    "module": "CommonJS",
    "esModuleInterop": true,
    "outDir": "dist",
    "noEmit": false,
    "declaration": true,
    "strict": true,
    "skipLibCheck": true,
    "lib": ["DOM", "ESNext"],
    "jsx": "react",
    "types": ["jest", "node"],
    "allowJs": true
  },
  "include": ["src", "assets"],
  "exclude": ["dist", "scripts", "node_modules"]
}

Was you able to create a custom render without the problem? If yes, could you share it?

@pierrezimmermannbam
Copy link
Collaborator

You see this error because you have declaration: true in your tsconfig.json. What happens is that TS needs to be able to name types in declaration files and since DebugFunction is not exported it cannot name it. Is there a reason why you'd want to generate declaration files? If not you can change your tsconfig to have declaration: false and noEmit: true.

I still believe that this type should be exported though since it's part of the public API and there may be valid use cases for wanting to generate declaration files

@mdjastrzebski mdjastrzebski merged commit 1c0fc79 into callstack:main Jul 17, 2023
@FelipeESchmidt
Copy link
Contributor Author

@pierrezimmermannbam Thank you very much for the explanation! I really don't know if the project's configuration needs to be the way it currently is, since it's the application where I work and I wasn't there during the project's creation. But again, thank you so much!

@mdjastrzebski
Copy link
Member

This PR has been released in v12.1.3 🚢

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 this pull request may close these issues.

3 participants