[英]Typescript ambient declaration namespace
Recently i have moved our spa app to typescript,and now i stuck with implementing the declation file of rsa.js which is used for openid authentication. 最近,我将水疗中心应用程序移至打字稿,现在我坚持实施用于openid身份验证的rsa.js声明文件。
Previously used Js code like below. 以前使用过的Js代码如下。
var jws = new KJUR.jws.JWS();
jws.verifyJWSByPemX509Cert(idToken, cert);
in a rsa.d.ts file 在rsa.d.ts文件中
declare namespace KJUR.jws {
interface IParsedJWS {
payloadS: any
}
interface IJWS {
new ();
verifyJWSByPemX509Cert(idToken: string, cert: string): boolean;
parsedJWS: IParsedJWS;
}
}
declare var JWS: KJUR.jws.IJWS;
declare module "KJUR.jws.JWS" {
export = JWS;
}
Im not sure what im doing here.Please anyone guide me the right way.How can i define namespace like KJUR.jws.JWS to use new KJUR.jws.JWS();? 我不确定在这里做什么,请任何人指导我正确的方式。我如何定义像KJUR.jws.JWS这样的命名空间以使用新的KJUR.jws.JWS();?
Thanks 谢谢
First up, there's a bug in your IJWS interface definition. 首先,您的IJWS接口定义中存在一个错误。 It defines a class-like object (something with a constructor), but it doesn't say what the constructor returns, and the methods defined are attached to the interface (ie the class itself), not the the object returned by the constructor.
它定义了一个类类的对象(带有构造函数的东西),但没有说明构造函数返回的内容,并且定义的方法附加到接口(即类本身)上,而不是构造函数返回的对象。 What you really want here is a class definition, not an interface:
您真正想要的是一个类定义,而不是一个接口:
class JWS {
verifyJWSByPemX509Cert(idToken: string, cert: string): boolean;
parsedJWS: IParsedJWS;
}
This more accurately represents what you actually have. 这可以更准确地表示您实际拥有的东西。 Once you've got that it's easy, you just need to export the class from inside the module ( playground link ):.
一旦知道了这很容易,您只需要从模块内部( 操场链接 )中导出类即可。
declare namespace KJUR.jws {
interface IParsedJWS {
payloadS: any
}
export class JWS {
verifyJWSByPemX509Cert(idToken: string, cert: string): boolean;
parsedJWS: IParsedJWS;
}
}
This works if you want to define an 'ambient' module: a module that doesn't need explicit loading (ie types representing a global that's already going to exist at runtime). 如果您想定义一个“环境”模块,则该方法有效:不需要显式加载的模块(即,表示运行时已经存在的全局变量的类型)。
If you want to make this a module loadable by name, through Node modules/RequireJS/etc, you just need to move that definition into a separate file, use a module instead of a namespace, and add quotes around the name, as below playground : 如果你想使这个名字的模块加载,通过节点模块/ RequireJS /等等,你只需要该定义移动到一个单独的文件,而不是使用一个命名空间的模块,和周围的名字加引号,如下游乐场 :
// In a separate file, e.g. "KJUR.d.ts":
declare module "KJUR.jws" {
interface IParsedJWS {
payloadS: any
}
export class JWS {
verifyJWSByPemX509Cert(idToken: string, cert: string): boolean;
parsedJWS: IParsedJWS;
}
}
// Elsewhere:
// (The string below and the module name above need to match what RequireJS
// is expecting, or it'll compile, but fail to find the module at runtime.
// You can change them to anything you like, as long as they match.)
import jws = require("KJUR.jws");
var idToken: string;
var cert: string;
var jws = new jws.JWS();
jws.verifyJWSByPemX509Cert(idToken, cert);
The key difference is how this KJUR object actually gets loaded in JavaScript at runtime. 关键的区别在于,此KJUR对象实际上是如何在运行时加载到JavaScript中的。 If you need a module to be actively loaded for it to become available, you need to use the named module syntax (the 2nd option above).
如果您需要主动加载模块以使其可用,则需要使用命名模块语法(上面的第二个选项)。 If it's a global that's already going to exist (you've got a script tag that already adds it) then you should just use the first option.
如果它是一个已经存在的全局变量(您已经添加了一个脚本标签),则应该只使用第一个选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.