在默认出口和命名出口(循环出口)上都导出一个值

问题描述 投票:0回答:1

我最近正在使用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

我想修复类型定义。

typescript typescript-typings commonjs
1个回答
0
投票

我想为其他人解决此问题,并尝试将值导出/导入为默认导出和命名导出(带有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

我必须:

  1. 重命名类从// // 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的循环引用)]
  2. 将课程前的export import ...更改为export
  3. 声明名称空间
  4. 在文件底部具有新名称declare(这称为名称空间合并)
  5. 将所有其他接口和类型移动到名称空间
  6. 并删除StrategyInternal
  7. 更新类中的引用
  8. 例如。 exportVerifyFunction
  9. 在名称空间中使用StrategyInternal.VerifyFunction
  10. 将值导出为命名导出
  11. 设置默认导出
  12. ,其中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');
© www.soinside.com 2019 - 2024. All rights reserved.