0
votes

I am trying to make a typescript definitions file for the package passport-twitchtv but am unable to seem to get it to find the definition.

Example definition file:

/// <reference types="passport"/>

import passport = require('passport');
import express = require('express');

interface Profile extends passport.Profile {
    id: string;
    username: string;
    displayName: string;
    email: string;

    _raw: string;
    _json: any;
}

interface IStrategyOptionBase {
    clientID: string;
    clientSecret: string;
    callbackURL: string;
    scope: string;
}

interface IStrategyOption extends IStrategyOptionBase {
    passReqToCallback?: false;
}

interface IStrategyOptionWithRequest  extends IStrategyOptionBase {
    passReqToCallback: true;
}

declare class Strategy extends passport.Strategy {
    constructor(options: IStrategyOption,
        verify: (accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) => void);
    constructor(options: IStrategyOptionWithRequest,
        verify: (req: express.Request, accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) => void);

    name: string;
    authenticate(req: express.Request, options?: Object): void;
}

Import method:

import { Strategy as TwitchStrategy } from 'passport-twitchtv';

I am getting the error: "Could not find a declaration file for module 'passport-twitchtv'".

If I drop the file in my node_modules/@types/passport-twitchtv it works, but I am unable to get typescript to find the .d.ts file otherwise.

I've tried adding a typeRoots to the compilerOptions in tsconfig.json, adding a typings.json file, adding "typings": "./typings/index" to the package file. Nothing I try seems to be working.

Not sure if I am supposed to declare the module when not in the node_modules/@types folder or not.

1

1 Answers

1
votes

Indeed, either your declaration file has to be findable by the normal module resolution process or you need to declare the module like this:

declare module "passport-twitchtv" {
    // All your original code:
    import passport = require('passport');
    import express = require('express');

    interface Profile extends passport.Profile {
        // ...
    }
    // etc.
}

The module resolution process looks in node_modules/@types (although it isn't good to modify that directory by hand since npm may blow away your changes), and you can get it to look in additional places using the baseUrl and paths compiler options; see the documentation.