首页 > 解决方案 > Adding custom definitions file for passport-twitchtv

问题描述

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.

标签: node.jstypescript

解决方案


实际上,您的声明文件必须可以通过正常的模块解析过程找到,或者您需要像这样声明模块:

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

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

模块解析过程查找(尽管手动修改该目录并不好,因为 npm 可能会破坏您的更改),您可以使用和编译器选项node_modules/@types让它查找其他位置;请参阅文档baseUrlpaths


推荐阅读