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.

syntaxhighlighter.d.ts - error TS7016: Could not find a declaration file for module 'react-syntax-highlighter/dist/cjs/create-element'.

See original GitHub issue

Describe the bug I’m using 6.0.0-rc.14 with Typescript and I have (and would like to keep) ‘no-implicitly-any’ enabled in my tsconfig.json. After adding a some components from @storybook/addon-docs/blocks I start to get implicitly has an 'any' type. error when type-checking my project. (via tsc --no-emit).

node_modules/@storybook/components/dist/syntaxhighlighter/syntaxhighlighter.d.ts:3:27 - error TS7016: Could not find a declaration file for module 'react-syntax-highlighter/dist/cjs/create-element'. '...MY_PROJECT_PATH/node_modules/react-syntax-highlighter/dist/cjs/create-element.js' implicitly has an 'any' type.

To Reproduce Steps to reproduce the behavior:

  1. In a simple Typescript React project run npx sb@next init.
  2. tsconfig.json should have the following setting enabled:
"noImplicitAny": true,
"noImplicitThis": true,
  1. Confirm version 6.0.0-rc.14 in package.json of following devDependencies:
 "@storybook/addon-actions": "^6.0.0-rc.14",
 "@storybook/addon-essentials": "^6.0.0-rc.14",
 "@storybook/addon-links": "^6.0.0-rc.14",
 "@storybook/react": "^6.0.0-rc.14",
  1. Add a simple MyComponent via 'MyComponent.tsc`.
import React from "react";

export const MyComponent = () => (<div>Woot!</div>);
  1. Add 'MyComponent.story.tsc` file that uses custom blocks.
import {
  Description,
  Props,
  Subtitle,
  Title,
} from "@storybook/addon-docs/blocks";
import React from "react";

export default {
  title: "Components/MyComponent",
  components: { MyComponent },
  parameters: {
    docs: {
      page: () => (
        <>
          <Title />
          <Subtitle />
          <Description
            markdown={`
# A Title
A description.
`}
          />
        </>
      ),
    },
  },
};

export const Basic = <MyComponent />
Basic.args = {};
  1. Run a project level TSC type-check via tsc --noEmit

Expected behavior TSC output of: Found 0 errors.

System:

Environment Info:

System: OS: macOS High Sierra 10.13.6 CPU: (8) x64 Intel® Core™ i7-4980HQ CPU @ 2.80GHz Binaries: Node: 10.19.0 - ~/.nvm/versions/node/v10.19.0/bin/node npm: 6.13.4 - ~/.nvm/versions/node/v10.19.0/bin/npm Browsers: Chrome: 84.0.4147.89 Firefox: 63.0.3 Safari: 13.1.2 npmPackages: @storybook/addon-actions: ^6.0.0-rc.14 => 6.0.0-rc.14 @storybook/addon-essentials: ^6.0.0-rc.14 => 6.0.0-rc.14 @storybook/addon-links: ^6.0.0-rc.14 => 6.0.0-rc.14 @storybook/react: ^6.0.0-rc.14 => 6.0.0-rc.14

Additional context I initially tried to npm install --save-dev @types/react-syntax-highlighter; however, the latest version doesn’t define the module, so I just added declare module "react-syntax-highlighter/dist/cjs/create-element"; to a project level *.d.ts file as a work-around.

The issues is likely being masked by this @ts-ignore https://github.com/storybookjs/storybook/blob/next/lib/components/src/syntaxhighlighter/syntaxhighlighter.tsx#L18-L19.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
Jessidhiacommented, Aug 17, 2020

A workaround; put in a non-module .d.ts file and reference it somehow.

declare module 'react-syntax-highlighter/dist/cjs/create-element' {
  import React from 'react'

  export type SyntaxHighlightNode =
    | (TextNode & Partial<Record<keyof TagNode<any>, never>>)
    | (TagNode<any> & Partial<Record<keyof TextNode, never>>)

  export interface TextNode {
    type: 'text'
    value: string
  }

  export interface TagNode<T extends React.ElementType> {
    tagName: T
    properties: React.ComponentProps<T>
  }

  export interface CreateElementProps {
    node: SyntaxHighlightNode
    stylesheet: import('react-syntax-highlighter').SyntaxHighlighterProps['customStyle']
    style?: React.CSSProperties
    useInlineStyles?: boolean
    key: string | number
  }

  export default function createElement(props: CreateElementProps): string | JSX.Element
}
0reactions
AndrewOCCcommented, Jan 12, 2021

I’ve also encountered this issue, @kaiyoma I can confirm the latest version of @types/react-syntax-highlighter does not have necessary type definitions for create-element ([definition].(https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a84a573295f22d04985033735efe6cb31a70f6bb/types/react-syntax-highlighter/index.d.ts))

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix error TS7016: Could not find a declaration file for ...
The second part of the error says: Try `npm install @types/XYZ` if it exists or add a new declaration (.d.ts) file containing `declare...
Read more >
Could not find a declaration file for module 'module-name ...
In Node.js everything works fine, but TypeScript: import {Injector} from '@ts-stack/di' ...
Read more >
Fixing the TS7016 Error | Atomist Blog
Could not find declaration file -- How do you get past the TS7016? This article highlights the eight best and worst fixes.
Read more >
Open Q&A - Algolia Community
Could not find a declaration file for module ... declaration (.d.ts) file containing declare module 'instantsearch.js/es/widgets'; ts(7016).
Read more >
Could not find a declaration file for module 'mongodb ... - Reddit
Yes I can create an "index.d.ts" file and include it there, but I actually want the types. I want to know what MongoClient.connect...
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