简体   繁体   English

如何在打字稿中输入文字?

[英]How to import type in typescript?

I have following export: 我有以下出口:

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

Then, i try to use it like so: 然后,我尝试像这样使用它:

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

  ){}

I'm getting this error, that pointing to JsonWebToken : 我收到此错误,指向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)

Nest's dependency injection is not designed to work with non-DI types (such as the JWT library). Nest的依赖项注入不适用于非DI类型(例如JWT库)。 My recommendation would be to make a wrapper component, using the jwt library according to ITS docs, then inject your wrapper to your controllers normally. 我的建议是根据ITS文档使用jwt库制作包装器组件,然后将包装器正常注入控制器。

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

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

After some diggin in i got next solution: 经过一番挖掘之后,我得到了下一个解决方案:

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

But this solution is dumb and makes me sad. 但是这种解决方法是愚蠢的,让我感到难过。 Is this a typescript bug or what? 这是打字稿错误还是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM