node.js - 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.
解决方案
实际上,您的声明文件必须可以通过正常的模块解析过程找到,或者您需要像这样声明模块:
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
让它查找其他位置;请参阅文档。baseUrl
paths
推荐阅读
- git - Git 默认 URL 问题
- ios - ios底部的滚动视图空白
- html - 在引导程序 4.1 中使用背景颜色垂直居中对齐 col 文本
- java - jackson ObjectMapper 需要序列化一个值,而不是返回null,而是不同的对象,有不同的表示
- python - 如何使用python将传感器数据输出保存到excel中
- python - Python - 通过函数传递变量
- php - 选择类别的产品包括所有父母
- java - Java替换字符串开头和结尾的所有$符号
- wordpress - Wordpress 网站偶尔会变成黑白
- combinations - 我们如何从 3000 个单词的语料库中找到第 n 个 3 个单词的组合