Search code examples
typescriptdefinitelytyped

Error while using DefinitelyTyped definition file


I am using showdownjs, and TypeScript. There is a definition file for the library from DefinitelyTyped;

The problem is that I think this library is wrong. The documentation calls for a converter to be instantiated like this;

var converter = new showdown.Converter();

But doing so with the TypeScript definition yields an error message telling it doesn't exist - because the namespace in the .d.ts is Showdown, but the library exists on global literal showdown.

The definition exports a module named showdown at the bottom, but this doesn't seem to be picked up by my IDE.

So my question: is this how TypeScript is supposed to work, or is it a good idea for me to try fixing the definition and submit a pull request to DefinitelyTyped?

Here is an example of how I am using showdown;

import 'angular';
import showdown from 'showdown';

export class MarkdownConfig {
    constructor($showdownProvider: any) {
        showdown.extension('notice', () => {
            return {
                type: 'lang',
                regex: /^(^\S+)\s+(>)\s+(.*)/gmi,
                replace: (text: string, css: string, method: string, message: string) => {
                    let converter = new showdown.converter();
                    return text.replace(/^(^\S+)\s+(>)\s+(.*)/gmi,
                             `<div class="notice notice-${css}">${message}</div>`);
                }
            };
        });
        $showdownProvider.loadExtension('notice');
    }
}

Solution

  • As the showdown.d.ts currently in DefinitelyTyped says, is target to version 0.3.1:

    // Type definitions for Showdown 0.3.1
    

    I ran into the same problem today, and issued a Pull Request targeting showdown 1.4.1 here.

    Before the PR been merged, you can install the type definition from this location:

    github:tan9/DefinitelyTyped/showdown/showdown.d.ts#aaaafcf9ba0e3d0a769bad1bbd55fbd38fdfbf48
    

    Then in your .ts files:

    import * as showdown from 'showdown';
    
    // register global extension
    showdown.extension('notice', () => {...})
    
    // use converter option to specify extension
    let converter = new showdown.Converter({extensions: ['notice']});
    
    // or use useExtension
    converter.useExtension('notice');