我最近正在使用passport-aut0
处理TypeScript项目。我用the DefinitelyTyped @types/passport-auth0
declaration file安装了它的类型。
从@types/passport-auth0
导入Strategy
作为命名导入效果很好(我的tsconfig中有passport-auth0
:]
"esModuleInterop": true
但是import {Strategy} from 'passport-auth0';
也被导出为默认的Strategy
。因此,就像在export
中一样,您应该可以这样导入它:
the Readme
这不适用于import Strategy from 'passport-auth0';
。错误消息是:
@types/passport-auth0
我想修复类型定义。
我想为其他人解决此问题,并尝试将值导出/导入为默认导出和命名导出(带有Type 'typeof import("/home/vsts/work/1/s/types/passport-auth0/index")' has no construct signatures.
的提示)。”>
我的解决方案(请参阅@nathan-shively-sanders)最终如下。
原始类型定义看起来像这样:
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/42315我必须:
//
// The class that will be exported / imported as both:
//
// - name export (works already because `export class`)
// - default export (doesn't work)
//
export class Strategy extends passport.Strategy { /* ... */ }
export interface Profile extends passport.Profile { /* ... */ }
export interface AuthenticateOptions extends passport.AuthenticateOptions { /* ... */ }
export interface StrategyOption { /* ... */ }
export interface StrategyOptionWithRequest extends StrategyOption { /* ... */ }
export interface ExtraVerificationParams { /* ... */ }
export type VerifyFunction = ( /* ... */ ) => void;
export type VerifyFunctionWithRequest = ( /* ... */ ) => void;
到Strategy
(以便在使用StrategyInternal
时可以导出不带Circular definition of import alias
的循环引用)]export import ...
更改为export
declare
(这称为名称空间合并)StrategyInternal
export
至VerifyFunction
StrategyInternal.VerifyFunction
export import
位于文件底部这是我最终解决方案的要点:
export = StrategyInternal
我只是想将其发布为解决方案,因为它似乎通过了测试并允许使用两种样式的ESM导入(在tsconfig中为
declare class StrategyInternal extends passport.Strategy { // Updated references to types and interfaces // Eg. changing `VerifyFunction` to `StrategyInternal.VerifyFunction` } declare namespace StrategyInternal { interface Profile extends passport.Profile { /* ... */ } interface AuthenticateOptions extends passport.AuthenticateOptions { /* ... */ } interface StrategyOption { /* ... */ } interface StrategyOptionWithRequest extends StrategyOption { /* ... */ } interface ExtraVerificationParams { /* ... */ } type VerifyFunction = ( /* ... */ ) => void; type VerifyFunctionWithRequest = ( /* ... */ ) => void; // NOTE: not true for `export import` statements // tslint:disable-next-line:strict-export-declare-modifiers export import Strategy = StrategyInternal; } export = StrategyInternal;
:]
"esModuleInterop": true
...或...
import {Strategy} from 'passport-auth0';
如果不使用ES Modules互操作,还支持以下样式:
import Strategy from 'passport-auth0';
...或...
import Strategy = require('passport-auth0');
或在JavaScript中使用CommonJS:
import {Strategy} = require('passport-auth0');
...或...
const Strategy = require('passport-auth0');