0

I'm trying to create a type definition for es6-promisify, a JS package not in the DefinitelyTyped annotations repository. Looking at examples in DefinitelyTyped and following TS Deep Dive / Declaration Files, I created a crude annotation that I saved in my project in a vendor.d.ts:

declare function promisify(original: (...args: any[]) => any, settings: any): Promise<any>

export = promisify
// export interface promisify { } // or should I do an interface?

Now, considering I import with import promisify = require('es6-promisify'), how do I tell TypeScript that the promisify import is annotated in vendor.d.ts? Currently, tsc keeps returning Could not find a declaration file for module 'es6-promisify'. 'promisify.js' implicitly has an 'any' type. I'm trying to digest TS Docs / Module Resolution but am failing at it so far.

Phrased differently: what's the mechanism used by TypeScript to resolve a declaration file from an import? XY problem warning: maybe I'm doing things wrong and shouldn't do a vendor.d.ts? Maybe there's a good reason es6-promisify is not in DT? Feel free to contradict with better ways to reach my goal of making tsc with "noImplicitAny": true happy. Thanks :)

5
  • 1
    Have you already tried renaming vendor.d.ts to es6-promisify.d.ts, and adding the directory containing the type definition it to compilerOptions.typeRoots in your tsconfig? If you are changing compilerOptions.typeRoots, don't forget to add ./node_modules/@types to it. refer: typescriptlang.org/docs/handbook/tsconfig-json.html Commented May 18, 2017 at 20:38
  • @SayanPal thanks; tried that, but I must still be doing something wrong, my import keeps failing to get types in vscode. Any method/tool to help me troubleshoot where the problem is? (config? declaration? something else?) Commented May 19, 2017 at 13:59
  • Not sure, you may try updating the TypeScript version and referring the correct version from vscode. It may happen that vscode is raising some false positive due to version mismatch, which might not be a run-time issue at the end. The error may also caused by how you are importing the module in your TypeScript code. It would be better if you post that code fragment as well. Commented May 19, 2017 at 14:11
  • @SayanPal thanks for the fast reponse :) . Well I'm using the latest TS 2.3.2, and sorry, which code fragment do you need? My post details both the declaration and the import line. Note: an additional thing I just tried: following DT's structure (example of such a simple typing that works: mkdirp) with an es6-promisify folder and index.d.ts + tsconfig.json files inside. Still failing :-/ Commented May 19, 2017 at 14:15
  • 1
    My bad, missed the import. Please check if my posted answer helps. Commented May 19, 2017 at 14:51

1 Answer 1

2

The following works for me.

es6-promisifiy.d.ts:

declare module "es6-promisify" {
  export default function promisify(original: (...args: any[]) => any, settings: any): Promise<any>
}

usage:

import promisify from "es6-promisify";
...
const xyz = promisify(whatever, whatever);

tsconfig.json:

{
    "compilerOptions": {
        ...
        "typeRoots": [
            "./node_modules/@types",
            "./custom_typings"
        ]
    },
    ...
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Yay! Works great (except the typing is bad, but now I can iterate on it 🙂). Looks like the declaration I shared missed wrapping the export in a declare module block. But then I don't understand why the mkdirp typings work without declare module, any idea? Thanks again!
@RonanJouchet Not exactly sure, but my closest guess is this depends on the module system (and resolution) you are using. But again, I am not sure. Maybe try opening a new question, and I will subscribe, as I am also interested to know the answer :) Have fun

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.