highlight.js: Typescript cannot locate the type information of the core module.

Hi there πŸ‘‹

I’d like to import the core and only necessary language files. I tried as below but then realised that there is something wrong in package regarding typings.

import hljs from 'highlight.js/lib/core'
import javascript from 'highlight.js/lib/languages/javascript'
Could not find a declaration file for module 'highlight.js/lib/core'. 

I have not imported the index module i.e. import hljs from 'highlight.js. So, typescript can’t locate the type information for the core module from a subfolder. I believe the solution might be creating .d.ts file for every subfolder and file in your case.

I quickly fixed this issue locally by creating a core.d.ts file under lib directory. So, I guess we need to declare the entire API that the library exposes.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 37 (24 by maintainers)

Most upvoted comments

FYI setting reference seems to play nice as near as I can tell.

In my case the file importing core is in src/filename.ts

/// <reference path="../node_modules/highlight.js/types/index.d.ts" />
import hljs from 'highlight.js/lib/core';

From there register your languages:

import typescript from 'highlight.js/lib/languages/typescript';
hljs.registerLanguage('typescript', typescript);

@joshgoebel When I ran cp -r /tmp/highlight.js/build ./node_modules/highlight.js, it worked

And index.d.ts can still live in the types folder with a reference in package.json types then?