【问题标题】:Adding custom definitions file for passport-twitchtv为 passport-twitchtv 添加自定义定义文件
【发布时间】:2019-03-18 18:49:53
【问题描述】:

我正在尝试为包 passport-twitchtv 制作打字稿定义文件,但似乎无法让它找到定义。

示例定义文件:

/// <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 { Strategy as TwitchStrategy } from 'passport-twitchtv';

我收到错误消息:“找不到模块 'passport-twitchtv' 的声明文件”。

如果我将文件放到我的 node_modules/@types/passport-twitchtv 中它可以工作,但我无法让打字稿找到 .d.ts 文件。

我尝试将 typeRoots 添加到 tsconfig.json 中的 compilerOptions,添加 typings.json 文件,将 "typings": "./typings/index" 添加到包文件中。我尝试的任何方法似乎都不起作用。

不确定我是否应该在不在 node_modules/@types 文件夹中时声明模块。

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

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

    declare module "passport-twitchtv" {
        // All your original code:
        import passport = require('passport');
        import express = require('express');
    
        interface Profile extends passport.Profile {
            // ...
        }
        // etc.
    }
    

    模块解析过程在 node_modules/@types 中查找(尽管手动修改该目录并不好,因为 npm 可能会破坏您的更改),您可以使用 baseUrlpaths 编译器选项;见the documentation

    【讨论】:

    • 抱歉,我仍然没有真正关注我应该在哪里添加declare module "passport-twitchtv",以及我应该用它包装什么。
    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 2013-11-21
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    相关资源
    最近更新 更多