2017-12-11 9 views
0

typecriptで型をインポートするには?私は輸出を以下している

その後
import * as jwt from 'jsonwebtoken'; 
    ... 
export type JsonWebToken = typeof jwt; 

、私はそうのようにそれを使用しよう:

export class AuthService { 
    constructor(
    @Inject(constants.JWT) private readonly jsonWebToken: JsonWebToken, 
    //           error here^ 

){} 

私はJsonWebTokenを指していることを、このエラーを取得しています:

ReferenceError: jwt_provider_1 is not defined 
    at Object.<anonymous> (D:\Learning\nest\project\server\modules\Auth\auth.service.ts:9:59) 
    at Module._compile (module.js:573:30) 
    at Module.m._compile (D:\Learning\nest\project\node_modules\ts-node\src\index.ts:392:23) 
    at Module._extensions..js (module.js:584:10) 
    at Object.require.extensions.(anonymous function) [as .ts] (D:\Learning\nest\project\node_modules\ts-node\src\index.ts:395:12) 
    at Module.load (module.js:507:32) 
    at tryModuleLoad (module.js:470:12) 
    at Function.Module._load (module.js:462:3) 
    at Module.require (module.js:517:17) 
    at require (internal/module.js:11:18) 

答えて

0

Nestの依存性注入は、非DI型(JWTライブラリなど)では機能しません。私のお勧めは、ITSのドキュメントに従ってjwtライブラリを使用してラッパーコンポーネントを作成し、通常はラッパーをコントローラに挿入することです。

@Component() 
export class JWTComponent { 
    signJwt(data) { 
     jwt.sign() // etc. 
    } 

    verifyJwt() { 
     jwt.verify() // etc. 
    } 
} 
+0

私は 'public sign = jwt.sign'でこれを行いました。すべて正常です。 –

-1

私は次の解決策を得た:dig30の後に:

import * as types from '...'; 
// then you can use it like so: 
jsonWebToken: types.JsonWebToken, 

しかし、この解決策はダムであり、私は悲しいです。これはタイスクリプトのバグですか、それとも何ですか?

関連する問題