[Compiler Bug]: TypeScript function overloads cause an exported function from a hook to be `undefined`
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 251k
- Forks
- 51.4k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 53
Description
What kind of issue is this?
- React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
- babel-plugin-react-compiler (build issue installing or using the Babel plugin)
- eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
- react-compiler-healthcheck (build issue installing or using the healthcheck script)
Link to repro
Repro steps
This only occurs when using babel-plugin-react-compiler version 19.1.0-rc.3, when using version 19.1.0-rc.2 it works.
When you define a function inside a component that has some TS function overloads when you export that function it is undefined in the compiled code. i.e.
// Hook
export function useTranslatedResourceOverload<Translations extends Record<string, MessageDescriptor>>(
translations: Translations,
) {
const { formatMessage } = useIntl();
function formatValue(key: keyof Translations): string;
function formatValue(key: keyof Translations | null | undefined): string | null;
function formatValue(key: keyof Translations | null | undefined) {
if (!key) return null;
if (!translations[key]) return key;
return formatMessage(translations[key]);
}
return { formatValue };
}
// Usage
const { formatValue } = useTranslatedResource(premiumTranslations)
// `formatValue` is now `undefined` here, so it cannot be called.
const premium = formatValue('none')
If you check the playground, the difference is that without the overloads, formatValue is assigned to t0, while with the function overloads, it never gets assigned to a tX variable.
I hope the info provided is enough.
How often does this bug happen?
Every time
What version of React are you using?
19.1.1
What version of React Compiler are you using?
19.1.0-rc.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked React Compiler playground and compare babel-plugin-react-compiler 19.1.0-rc.2 with 19.1.0-rc.3 for the overloaded hook example. Inspect the compiled output around the overloaded function and verify that the exported function is assigned and callable rather than undefined.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- babel, react, typescript
- Domain
- compilers, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100