question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated .d.ts does not import destructure react imports

See original GitHub issue

Bug report

Below I have supplied a minimal repo. In our project we destructure react types in our files. The output of the bundle does not include these imports causing a malformed d.ts

Input code

import React, { ReactElement } from 'react';

export function Test(): ReactElement {
  return <div />;
}

export const ForwardedRef = React.forwardRef<HTMLDivElement>((props, ref) => <div ref={ref} {...props} />)

Expected output

// Generated by dts-bundle-generator v5.9.0

/// <reference types="prop-types" />
/// <reference types="react" />
/// <reference types="scheduler" />
import { ReactElement } from "react";

export declare function Test(): ReactElement;
export declare const ForwardedRef: React.ForwardRefExoticComponent<React.RefAttributes<HTMLDivElement>>;

export {};

Actual output

// Generated by dts-bundle-generator v5.9.0

/// <reference types="prop-types" />
/// <reference types="react" />
/// <reference types="scheduler" />

export declare function Test(): ReactElement;
export declare const ForwardedRef: React.ForwardRefExoticComponent<React.RefAttributes<HTMLDivElement>>;

export {};

Additional context Add any other context about the problem here (CLI options, etc)

tsconfig.json

{
  "compilerOptions": {
    "jsx": "react",
    "lib": ["DOM", "ES2015", "ES2016"],
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

package.json

{
  "dependencies": {
    "dts-bundle-generator": "^5.9.0",
    "react": "^17.0.2",
    "@types/react": "^17.0.19"
  }
}

Command

› dts-bundle-generator index.tsx -o index.d.ts
Compiling input files...
Processing index.tsx
Library "react" will be added via reference directive
Library "prop-types" will be added via reference directive
Library "scheduler" will be added via reference directive
Writing index.tsx -> index.d.ts
Checking generated files...
Compiler option "skipLibCheck" is disabled to properly check generated output
index.d.ts(7,33): error TS2304: Cannot find name 'ReactElement'.

Error: Compiled with errors

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
igorpupkinablecommented, Oct 20, 2021

Thank you for the details.

Perhaps I need to rephrase my question. allowedTypesLibraries: ['react'] option will only work with const HelloWorld: React.FC = () => null; code.

My library has over 100 components with FC type instead of React.FC. So importedLibraries: ['react'] is the only working solution otherwise you get lib/index.d.ts(1,18): error TS2304: Cannot find name 'FC'. error. FC must be imported in .d.ts file.

1reaction
timocovcommented, Oct 20, 2021

allowedTypesLibraries: [‘react’] option will only work with const HelloWorld: React.FC = () => null; code.

Yep, that’s correct (and iirc this is the default behaviour for now right? I mean you don’t need to even specify allowedTypesLibraries explicitly to achieve this, after #170 it will be changed).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing support for destructuring in `import … = <namespace ...
TypeScript Version: 2.6.1 Intent To be able to export imports (using allowSyntheticDefaultImports) and not run into the dreaded voldemort ...
Read more >
Typescript can not import with out extention - Stack Overflow
I am basically a noob at Typescript and webpack and while learning it I faced with a Major Issue. The Import statement has...
Read more >
Refactor React Imports - JavaScript in Plain English
First I created a new file named Imports.js in my src folder. ... All you have to do now is pick which component...
Read more >
How To Use Modules in TypeScript | DigitalOcean
Since your src/vectors.ts is only importing two classes to later re-export them, you can use an even briefer form of the syntax:.
Read more >
Documentation - Modules - TypeScript
TypeScript Specific ES Module Syntax. Types can be exported and imported using the same syntax as JavaScript values: ts. // @filename: animal.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found