What to do if a typings (or tsd) is not available?
Asked Answered
I

2

20

I was looking over the TypeScript handbook and I can't seem to find the answer.

If I am using a library that no typings exist, what are my options?

One is to create the typings file, but this I don't really want to do.

What are my other options, I seem to remember some kind of 'declare' keyword ?

Or maybe something in the tsconfig ?

I assume there is a way of declaring this variable (type) globally so every time I use it, it would just work.

And I presume there is a way of just declaring it only available in one file.

Of course the best way would be to include a typings file but this won't always be available.

Inspiratory answered 25/4, 2016 at 7:13 Comment(0)
N
10

If your primary concern is just to get rid of the errors, you can simply write declare var MyUntypedLibrary: any; above your code, where MyUntypedLibrary is the name of the global reference to your dependency.

If you need this reference in several files and don't want to repeat yourself, you could write it in the top of any file, above any namespaces, and it would be available to the whole project. If you have many untyped dependencies, it would probably be a good idea to have a separate ts-file where you define these.

Note: This works fine when using local modules. I'd guess this might be more troublesome if one is using external modules and want to have one place to define an implicit dependency for them all, but then again each module should handle their own dependencies internally anyway.

Normanormal answered 25/4, 2016 at 10:16 Comment(0)
M
1

Although the best would be to create your own definition file (.d.ts), clone the https://github.com/DefinitelyTyped/DefinitelyTyped repository, add to it, and create a pull request back to their trunk; you way want a quicker solution here.

You could write your own simplest definition file corresponding to your need, and add it to your project, hence keeping you away from waiting that definitions are accepted / read to merge in DefTyped trunk.

However you could write your simple def file, and create the pull request anyway, small def file is better than no def file ;)

Additional instructions to create your definition : http://definitelytyped.org/guides/creating.html

Simple example:

 interface JQuery {

    /* wrapped plugins */

    dynatree(options: any): any;

    multiselect(): JQuery;
    multiselect(MultiSelectSettings): any;
    multiselect(command: string): any;

    ajaxSubmit(options: any);
    layout(options: any);

    colpick(options?: any);
    colpickHide();

    idcDataTable(options?: any);

    dragscrollable(options: any);

    /* wrapped plugins - END */
}

interface JQueryStatic {
    layout: any;

    fileDownload: any;

    pnotify: any;

    sha256(input: string): string;
}
Mccollough answered 25/4, 2016 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.