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

Generated TypeScript definitions file has broken import("") #8908

Closed
bvaughn opened this issue Mar 23, 2023 · 2 comments · Fixed by #9573
Closed

Generated TypeScript definitions file has broken import("") #8908

bvaughn opened this issue Mar 23, 2023 · 2 comments · Fixed by #9573

Comments

@bvaughn
Copy link

bvaughn commented Mar 23, 2023

Originally reported at bvaughn/react-error-boundary#133

Parcel version 2.8.3

Repro

The easiest way to repro this is to:

  1. Check out this repo: https://github.com/bvaughn/parceljs-typescript-bug
  2. Run yarn install && yarn build
  3. Look at dist/index.d.ts

Source

// index.ts
export * from "./ErrorBoundary";
export * from "./ErrorBoundaryContext";
// ErrorBoundaryContext.ts
import { createContext } from "react";

export type ErrorBoundaryContextType = {};

export const ErrorBoundaryContext = createContext<ErrorBoundaryContextType | null>(null);
// ErrorBoundary.ts
import { Component, createElement, PropsWithChildren } from "react";
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";

export class ErrorBoundary extends Component<PropsWithChildren> {
  render() {
    const { children } = this.props;

    return createElement(
      ErrorBoundaryContext.Provider,
      {
        value: {},
      },
      children
    );
  }
}

Output

// dist/index.d.ts
import { Component, PropsWithChildren } from "react";
export type ErrorBoundaryContextType = {};
export const ErrorBoundaryContext: import("react").Context<ErrorBoundaryContextType>;
export class ErrorBoundary extends Component<PropsWithChildren> {
  render(): import("react").FunctionComponentElement<
    import("react").ProviderProps<
      import("ErrorBoundaryContext").ErrorBoundaryContextType
    >
  >;
}

Problem

Note the return type of the render method of ErrorBoundary – it tries to import ErrorBoundaryContext as though it were
a separate package/module, rather than a variable that's already in scope.

@bvaughn
Copy link
Author

bvaughn commented Mar 23, 2023

Note that a workaround for this issue seems to be declaring an explicit return type:

return createElement(
  ErrorBoundaryContext.Provider,
  {
    value: {},
  },
  children
) as ReactElement;
// dist/index.d.ts
export class ErrorBoundary extends Component<PropsWithChildren> {
    render(): ReactElement<any, string | import("react").JSXElementConstructor<any>>;
}

@devongovett
Copy link
Member

Hmm looks like TSC generates the dynamic import but we only handle import declarations (statements) here:

if (ts.isImportDeclaration(node) && node.importClause) {
Might need to add a pass to rewrite dynamic imports to regular import statements first if TSC is generating these...

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

Successfully merging a pull request may close this issue.

3 participants