Skip to content

[WIP] Emitting Declarations for React Components requires type annotation #1086

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 6, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original issue description:

When trying to emit declarations for a React package, tsgo requires type annotations for React Components.

Example component being used:

/* src/index.tsx */
export const MyComponent = () => {
  return <div>Hello World</div>
}

tsconfig.json being used:

{
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["node_modules"],
  "compilerOptions": {
    "target": "es6",
    "jsx": "react-jsx",
    "forceConsistentCasingInFileNames": true,
    "noEmit": false,
    "esModuleInterop": true,
    "incremental": false,
    "isolatedModules": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true
  }
}

Command:

tsgo -p tsconfig.json --emitDeclarationOnly --declaration --outDir out

The following command produces:

The inferred type of 'MyComponent' cannot be named without a reference to '../node_modules/@types/react/jsx-runtime.js'. This is likely not portable. A type annotation is necessary.

Running the same with tsc produces no errors.

Fixes #1011.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@jakebailey
Copy link
Member

@copilot try again

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 22, 2025

@copilot try again

I've implemented a fix for the jsx-runtime declaration emit issue. The problem was that jsx-runtime imports were being treated as unsafe node_modules references. I added a helper function to detect jsx-runtime related imports and exclude them from the unsafe import check. The fix handles multiple jsx-runtime path patterns and doesn't break existing tests. Commit 1b414f4.

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
@Copilot Copilot AI requested a review from jakebailey June 22, 2025 05:29
Copilot finished work on behalf of jakebailey June 22, 2025 05:29
@@ -0,0 +1,33 @@
// Test to ensure jsx-runtime imports are allowed in declaration files
Copy link
Member

Choose a reason for hiding this comment

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

Do not create new unit tests, use compiler tests like in testdata/cases, or fix existing tests in the submodule. See your instructions on how to run tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! Removed the unit test file and restored the compiler test in testdata/tests/cases/compiler/jsxRuntimeDeclarationEmit.tsx. The test runs correctly and verifies that jsx-runtime declaration emit no longer produces the unsafe import error. Commit 19b3ddb.

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
@Copilot Copilot AI requested a review from jakebailey June 22, 2025 05:57
Copilot finished work on behalf of jakebailey June 22, 2025 05:57
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.

Emitting Declarations for React Components requires type annotation
3 participants